Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions valid-anagram/hi-rachel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드를 구조화하여 세가지 방법을 모두 제공한 것이 매우 좋습니다 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@printjin-gmailcom 이번에 재수 2번째 도전이라 여러 풀이로 도전했습니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안그래도 꽤나 익숙한 아이디이긴했어요 ㅎㅎ 코드가 다 깔끔하고 좋아서 많이 배워갑니다다. 5기 화이팅 :)

https://leetcode.com/problems/valid-anagram/description/
두 문자열이 애너그램인지 확인하는 함수를 작성하세요.
애너그램이란 두 문자열이 중복된 알파벳을 같은 개수만큼 포함하고 있는 것을 의미합니다.
TC: O(n)
SC: O(k), k = 알파벳 개수
"""

from collections import defaultdict

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
def makeMap(s: str):
str_map = defaultdict(int)
for char in s:
str_map[char] += 1
return str_map

return makeMap(s) == makeMap(t)


"""
정렬 풀이
TC: O(nlogn)
SC: O(1)
"""
def isAnagram(s: str, t: str) -> bool:
return sorted(s) == sorted(t)

"""
Counter 사용 풀이
TC: O(n)
SC: O(k)
"""
from collections import Counter

def isAnagram(s: str, t: str) -> bool:
return Counter(s) == Counter(t)