-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20241127.py
More file actions
53 lines (40 loc) · 1.19 KB
/
20241127.py
File metadata and controls
53 lines (40 loc) · 1.19 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
52
53
'''
https://leetcode.com/problems/valid-anagram/description/
Valid Anagram
Given two strings s and t, return true if t is an
anagram of s, and false otherwise.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
'''
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
occurrence = {}
for char in s:
if char not in occurrence:
occurrence[char] = 0
occurrence[char] += 1
for char in t:
if char not in occurrence:
occurrence[char] = 0
occurrence[char] -=1
for key in occurrence:
if occurrence[key] != 0:
return False
return True
solution = Solution()
print(solution.isAnagram("anagram", "nagaram")) # -> True
print(solution.isAnagram("rat", "car")) # -> False
'''
Pseudocode:
- Declare an occurrence dict
- Iterate through the s string value and t string value respectively
- Add key of s to occurrence dict and increment value by one
- Add key of t to occurrence dict and decrement value by one
- Iterate through the keys in the occurrence dict
- If any of the values do not equal zero, return False
- Return true as default value
'''