-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcount_occurances_of_anagram.py
More file actions
64 lines (46 loc) · 1.47 KB
/
count_occurances_of_anagram.py
File metadata and controls
64 lines (46 loc) · 1.47 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
'''
Count Occurences of Anagrams- GeeksForGeeks
Problem Link: https://practice.geeksforgeeks.org/problems/count-occurences-of-anagrams/0
Author: Shyam Kumar
Date: 27th Sep, 2019
'''
def check_anagram(count_s, count_w):
""" This function returns True if the two list are identical """
str1="".join(map(str,count_s))
str2="".join(map(str,count_w))
if str1 == str2:
return True
def count_occurances_of_anagrams(S,w):
""" This function counts the number of occurences of anagrams in S """
# List of size 26 and initialized to 0 for counting chars in w
count_w=[0]*26 i
count=0 # count anagrams
# Count the characters in word w
for c in w:
count_w[ord(c)-97]+=1
l1=len(w)
l2=len(S)
# List of size 26 and initialized to 0 for counting chars in S
count_s=[0]*26
# Count the starting l1 chars in S
for i in range(l1):
count_s[ord(S[i])-97]+=1
if(check_anagram(count_s, count_w)):
count+=1
r=l2-l1+1
# Move and check for anagram at each iteration
for i in range(1,r):
count_s[ord(S[i-1])-97]-=1
count_s[ord(S[l1+i-1])-97]+=1
if(check_anagram(count_s,count_w)):
count+=1
return count
def main():
t=int(input())
for _ in range(t):
S=input()
w=input()
ans = count_occurances_of_anagrams(S,w);
print("Total no. of Anagrams: ",ans)
if __name__=="__main__":
main()