Valid Anagram

LeetCode

LeetCode: https://leetcode.com/problems/valid-anagram/

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

Example:

  • Input: s = "anagram", t = "nagaram"
  • Output: true
1def isAnagram(s: str, t: str) -> bool:
2 if len(s) != len(t):
3 return False
4
5 count = {}
6
7 for c in s:
8 count[c] = count.get(c, 0) + 1
9
10 for c in t:
11 if c not in count:
12 return False
13 count[c] -= 1
14 if count[c] == 0:
15 del count[c]
16
17 return len(count) == 0
i
1
0
1
1
1
2
1
3
Step 1 / 3
Step 1:
Build frequency map from s.
Pointers: i=0
Focus: default