-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidAnagram.py
More file actions
81 lines (71 loc) · 1.76 KB
/
Copy pathValidAnagram.py
File metadata and controls
81 lines (71 loc) · 1.76 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
"""
Validate Anagrams
Find out if string S is an anagram of string T.
input
S = "the eyes"
T = "they see"
expected output
True
input 2
S = "a gentleman"
T = "elegant man"
expected output
True
Anagram - a word or a phrase formed by
rearranging the letters of another,
such as cinema, formed from iceman.
Time complexity: O(nlogn) because of using sorted (mergesort alg)
"""
def anagrams(S, T):
# the sorted strings are checked
if(sorted(S) == sorted(T)):
return True
else:
return False
S = "the eyes"
T = "they see"
assert anagrams(S, T) == True
S = "a gentleman"
T = "elegant man"
assert anagrams(S, T) == True
# solution - 2
def anagrams(S, T):
s_list = list(S)
s_list.sort()
t_list = list(T)
t_list.sort()
return (s_list == t_list)
S = "the eyes"
T = "they see"
assert anagrams(S, T) == True
S = "a gentleman"
T = "elegant man"
assert anagrams(S, T) == True
# solution - 3
# Time complexity: O(n+m)
NO_OF_CHARS = 256
# Function to check whether two strings are anagram of
# each other
def anagrams(S, T):
if len(S) != len(T):
return False
# Create two count arrays and initialize all values as 0
count1 = [0] * NO_OF_CHARS
count2 = [0] * NO_OF_CHARS
# For each character in input strings, increment count
# in the corresponding count array
for i in S:
count1[ord(i)]+= 1
for i in T:
count2[ord(i)]+= 1
# Compare count arrays
for i in range(NO_OF_CHARS):
if count1[i] != count2[i]:
return False
return True
S = "the eyes"
T = "they see"
assert anagrams(S, T) == True
S = "a gentleman"
T = "elegant man"
assert anagrams(S, T) == True