본문 바로가기

Algorithm/Leetcode

[Top Interview 150 - Hashmap] 242. Valid Anagram

[Q] [Medium]

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

 

  • 일단 문제가 짧아서 좋았다.
  • 문제가 짧으니까 잘 풀리는 것 같기도..?
  • count(i) 함수 써서 해당 '문자열' 의 개수를 value 로 넣어주니 쉽게 풀렸다.
# Answer

class Solution:
    def isAnagram(self, s: str, t: str) -> bool:

        # 길이 안 맞으면 False
        if len(s) != len(t):
            return False

        s_dict = dict()
        t_dict = dict()

        for i in s:
            s_dict[i] = s.count(i)

        for j in t:
            t_dict[j] = t.count(j)

        if s_dict == t_dict:
            return True

        return False