-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram_problem
More file actions
76 lines (45 loc) · 912 Bytes
/
Anagram_problem
File metadata and controls
76 lines (45 loc) · 912 Bytes
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
from collections import defaultdict
a = 'married'
b = 'admirer'
h1 = defaultdict(int)
h2 = defaultdict(int)
for i in range(len(a)):
h1[a[i]] = h1[a[i]] + 1
h2[b[i]] = h2[b[i]] + 1
if h1 == h2:
print("True")
else:
print("False")
a = 'married'
b = 'admirer'
c = sorted(a)
d = sorted(b)
left = 0
right = 0
for i in range(len(a)):
if c[left] == d[right]:
left +=1
right +=1
elif c[left] != d[right]:
break
if left == right:
print("True")
else:
print("False")
from collections import Counter
s = "cbaebabacd"
p = "abc"
n = len(p)
ana = s[:n]
save = [ana]
inc = 1
for i in range(n,len(s)):
output = s[inc:i+1]
inc +=1
save.append(output)
for i in range(len(save)):
if save[i] == p:
print(p)
for sub in save:
if Counter(sub) == Counter(p): # anagram check
print(f"Anagram found: {sub}")