-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0242__valid_anagram.py
More file actions
51 lines (35 loc) · 1.22 KB
/
0242__valid_anagram.py
File metadata and controls
51 lines (35 loc) · 1.22 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
"""
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.
## Example 1
Input: s = "anagram", t = "nagaram"
Output: true
## Example 2
Input: s = "rat", t = "car"
Output: false
## Constraints
* 1 <= s.length, t.length <= 5 * 10^4
* s and t consist of lowercase English letters.
## Follow up
What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
"""
from collections import defaultdict
from typing import Dict
from unittest import TestCase
class Solution(TestCase):
def test_example_1(self):
self.assertTrue(self.isAnagram("anagram", "nagaram"))
def test_example_2(self):
self.assertFalse(self.isAnagram("rat", "car"))
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
alphabet: Dict[str, int] = defaultdict(int)
for char_s, char_t in zip(s, t):
alphabet[char_s] += 1
alphabet[char_t] -= 1
for char in s:
if alphabet[char] != 0:
return False
return True