-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0242_valid_anagram.py
More file actions
87 lines (73 loc) · 2.18 KB
/
0242_valid_anagram.py
File metadata and controls
87 lines (73 loc) · 2.18 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
frequencies = [0] * 26
count = 0
for letter in s:
index = ord(letter) - ord('a')
frequencies[index] += 1
count += 1
for letter in t:
index = ord(letter) - ord('a')
if frequencies[index] == 0:
return False
frequencies[index] -= 1
count -= 1
return count == 0
"""
# Method 1: While Loop
flag = True
i = 0
# Check length of the string
if len(s) != len(t):
flag = False
# Check letters
while (flag == True) and (i < len(t)):
# Check t[i] is in s
if t[i] not in s:
flag = False
else:
a = s.find(t[i])
if a == 0:
s = s[a + 1:]
else:
s = s[:a] + s[a + 1:]
i += 1
return flag
"""
"""
# Method 2: Array Quick Sort
def quick_sort(array):
less = []
equal = []
greater = []
if len(array) > 1:
pivot = array[0]
for x in array:
if x < pivot:
less.append(x)
elif x == pivot:
equal.append(x)
elif x > pivot:
greater.append(x)
return quick_sort(less) + equal + quick_sort(greater)
else:
return array
array_s = []
array_t = []
# Check length of the string
if len(s) != len(t):
return False
# Make string into array with characters
for i in range(len(s)):
array_s.append(s[i])
array_t.append(t[i])
# Quick Sort
array_s = quick_sort(array_s)
array_t = quick_sort(array_t)
return array_s == array_t
"""