Leetcode # 242 (Python): Valid Anagram

정욱·2021년 4월 23일
0

Leetcode

목록 보기
23/38
post-custom-banner

Valid Anagram

  • Difficulty: Easy
  • Type: Sorting
  • link

Problem

Solution

  • Sort two words and check if they are equal
  • Time complexity: O(n log n)
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if sorted(s) == sorted(t):
            return True
        else:
            return False
  • Count every occurence of alphabets of two words and see if the counts are equal
  • Time complexity: O(n)
import collections
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        if collections.Counter(s) == collections.Counter(t):
            return True
        else:
            return False
post-custom-banner

0개의 댓글