Skip to main content

0242 Valid Anagram

Solved at: 2022-09-04


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

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.


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



  • 129 ms, faster than 6.44% of Python3 online submissions for Valid Anagram.

Memory Usageโ€‹

  • 15.1 MB, less than 11.95% of Python3 online submissions for Valid Anagram.

Complexity Analysisโ€‹

  • Time: O(nlogโกn)O(n \log n)
  • Space: O(1)O(1)

Other Answers Onlineโ€‹

  • Frequency Counter Map alphabet โ†’ count
    • Time: O(n)O(n)
    • Space: O(1)O(1) because the map's size is constant.