Problem Solving/LeetCode
2023.11.15 Today's Challenge
fabichoi
2023. 11. 15. 23:45
https://leetcode.com/problems/sort-vowels-in-a-string/?envType=daily-question&envId=2023-11-13
Sort Vowels in a String - LeetCode
Can you solve this real interview question? Sort Vowels in a String - Given a 0-indexed string s, permute s to get a new string t such that: * All consonants remain in their original places. More formally, if there is an index i with 0 <= i < s.length such
leetcode.com
set을 이용해서 푸는 문제.
문제에 대해서 이해를 좀 못하긴 함 ㅠ
class Solution:
def sortVowels(self, s: str) -> str:
freq = Counter(s)
vowels = list('uoieaUOIEA')
vowels_set = set(vowels)
res = []
for c in s:
if c in vowels_set:
while freq[vowels[-1]] == 0:
vowels.pop()
res.append(vowels[-1])
freq[vowels[-1]] -= 1
else:
res.append(c)
return ''.join(res)
반응형