[LeetCode] First Unique Character in a String 파이썬 (Counter, str.find)
from collections import Counter class Solution: def firstUniqChar(self, s: str) -> int: idx = -1 c = Counter(s) # print(c) idx_list = [] for k,v in c.items(): # print(k,v) if v == 1: idx = s.index(k) return idx else: return idx Counter 클래스를 사용해서 풀었다. counter를 찍어보니 Counter({'e': 3, 'l': 1, 't': 1, 'c': 1, 'o': 1, 'd': 1}) 이런식으로 value 순으로 descending order로 출력은 되는데 실제 items로 iterate 해보면 입력순으로, 즉, l..
2021. 4. 14.
[LeetCode] Reverse Vowels of String 파이썬 (swap, two pointer)
reverse string 문제에서 진화된 문제 i, j = 0, len(s) - 1 while i < j: s[i], s[j] = s[j], s[i] i += 1 j -= 1 2개의 변수(i,j)들을 활용해서 swap dict나 list 뿐만 아니라 string 도 not in 구문이 가능하다는 점. i는 forward j는 backward 탐색 ㄱ vowel의 위치를 찾을 경우 i,j swap class Solution(object): def reverseVowels(self, s): """ :type s: str :rtype: str """ vowels = "aeiou" string = list(s) i, j = 0, len(s) - 1 while i < j: if string[i].lower() ..
2021. 4. 13.