forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis_anagram.py
More file actions
39 lines (30 loc) · 847 Bytes
/
is_anagram.py
File metadata and controls
39 lines (30 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Is Anagram
Determine whether two strings are anagrams of each other by comparing
character frequency maps.
Reference: https://leetcode.com/problems/valid-anagram/description/
Complexity:
Time: O(n)
Space: O(n)
"""
from __future__ import annotations
def is_anagram(s: str, t: str) -> bool:
"""Check if string t is an anagram of string s.
Args:
s: First string.
t: Second string.
Returns:
True if t is an anagram of s, False otherwise.
Examples:
>>> is_anagram("anagram", "nagaram")
True
>>> is_anagram("rat", "car")
False
"""
freq_s: dict[str, int] = {}
freq_t: dict[str, int] = {}
for char in s:
freq_s[char] = freq_s.get(char, 0) + 1
for char in t:
freq_t[char] = freq_t.get(char, 0) + 1
return freq_s == freq_t