-
Notifications
You must be signed in to change notification settings - Fork 0
/
(Algorithm)Bear_and_Steady_Gene(Two_Points_Dict).py
50 lines (45 loc) · 1.29 KB
/
(Algorithm)Bear_and_Steady_Gene(Two_Points_Dict).py
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
'''
Algorithm: Two Pointers
'''
#!/bin/python3
import sys
def steadyGene(gene):
length = len(gene)
limit = length // 4
count_gene = {'A':0, 'C':0, 'G':0, 'T':0}
for c in gene:
count_gene[c] += 1
for key, value in count_gene.items():
if value > limit:
count_gene[key] = value - limit
else:
count_gene[key] = 0
num_2re = sum(count_gene.values())
if num_2re == 0:
return 0
start, end = 0, 0
res = length
count_sub = {'A':0, 'C':0, 'G':0, 'T':0}
while end < length:
hasFound = True
for k in count_sub:
if count_sub[k] < count_gene[k]:
hasFound = False
break
if hasFound == True:
# required substring has found update minimum length res
if res > end - start:
res = end - start
# increase start pointer to less the length of substring if possible
count_sub[gene[start]] -= 1
start += 1
elif hasFound == False:
# required substring not found yet
count_sub[gene[end]] += 1
end += 1
return res
if __name__ == "__main__":
n = int(input().strip())
gene = input().strip()
result = steadyGene(gene)
print(result)