파비의 매일매일 공부기록

2023.11.15 Today's Challenge 본문

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)
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.11.17 Today's Challenge  (0) 2023.11.17
2023.11.16 Today's Challenge  (0) 2023.11.16
2023.11.14 Today's Challenge  (0) 2023.11.14
2023.11.13 Today's Challenge  (0) 2023.11.13
2023.11.12 Today's Challenge  (0) 2023.11.12
Comments