-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid Anagram.py
More file actions
53 lines (39 loc) · 1.8 KB
/
Valid Anagram.py
File metadata and controls
53 lines (39 loc) · 1.8 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
#Given two strings s and t , write a function to determine if t is an anagram of s.
#Solution 1
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
#Initialize a list of length 26 filled with 0s
#let a map to 0, b map to 1... z map to 25
charList = [0]*26
#if the two strings differ in length, return false
if len(s) != len(t):
return False
#for each letter in the string
for letterIndex in range(len(s)):
#add one to the corresponding letter index if it is in string s
charList[ord(s[letterIndex])-97] = charList[ord(s[letterIndex])-97] + 1
#subtract one to the corresponding letter index if it is in string t
charList[ord(t[letterIndex])-97] = charList[ord(t[letterIndex])-97] - 1
#if the letter frequencies do not differ, return True
if charList == [0]*26:
return True
else:
False
#Solution 2
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
#Initialize a list of length 26 filled with 0s
#let charList[i] store the frequency of a unique letter from a to z
#index 0 to 25
charList = [0]*26
#if the two strings differ in length, return false
if len(s) != len(t):
return False
#for each letter in the string s
for letterIndex in range(len(s)):
charList[ord(s[letterIndex])-97] = charList[ord(s[letterIndex])-97] + 1
for letterIndex in range(len(t)):
charList[ord(t[letterIndex])-97] = charList[ord(t[letterIndex])-97] - 1
if charList[ord(t[letterIndex])-97] < 0:
return False
return True