src.valid_anagram

Classes

Solution()

class src.valid_anagram.Solution[source][source]
isAnagram(s: str, t: str) bool[source]

Thought process

  • Trivially if the words have a different length we return False

  • We create two dictionaries to store the frequency of each character in the words

  • We compare the dictionaries to see if they are equal

Notes

  • time complexity: \(O(n)\)

  • space complexity: \(O(n)\)

isAnagramSort(s: str, t: str) bool[source]

Thought process

  • Trivially if the words have a different length we return False

  • We sort the words and compare them

Notes

  • time complexity: \(O(n^2)\) -> \(O(n\log n)\)

  • space complexity: \(O(n)\) -> \(O(1)\)

isAnagramCounter(s: str, t: str) bool[source]

A bit of a cheat, but we can use the Counter class from the collections module