[LeetCode][Python3] 438. Find All Anagrams in a String
2018. 10. 7. 22:38 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
My Solution :
class Solution:
def findAnagrams(self, s, p):
"""
:type s: str
:type p: str
:rtype: List[int]
"""
ans = []
p_counter = [0]*26
for c in p:
p_counter[ord(c)-97] += 1
size = len(p)
s_counter = [0]*26
for i, c in enumerate(s):
s_counter[ord(c)-97] += 1
if size <= i:
s_counter[ord(s[i-size])-97] -= 1
if p_counter == s_counter:
ans.append(i-size+1)
return ans
Comment :
알파벳 소문자로만 구성되어 있다고 해서 이렇게 풀었다. 그게 아니었다면 Dictionary(HashMap)를 활용했을 것이다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 617. Merge Two Binary Trees (0) | 2018.10.08 |
---|---|
[LeetCode][Python3] 581. Shortest Unsorted Continuous Subarray (0) | 2018.10.08 |
[LeetCode][Python3] 572. Subtree of Another Tree (0) | 2018.10.07 |
[LeetCode][Python3] 538. Convert BST to Greater Tree (0) | 2018.10.07 |
[LeetCode][Python3] 437. Path Sum III (0) | 2018.10.07 |
[LeetCode][Python3] 283. Move Zeroes (0) | 2018.10.05 |
[LeetCode][Python3] 234. Palindrome Linked List (0) | 2018.10.04 |
[LeetCode][Python3] 226. Invert Binary Tree (0) | 2018.10.03 |
최근에 달린 댓글 최근에 달린 댓글