-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek3-medium4.py
More file actions
54 lines (43 loc) · 1.26 KB
/
week3-medium4.py
File metadata and controls
54 lines (43 loc) · 1.26 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
# algorithms -> strings -> bear and steady
# https://www.hackerrank.com/challenges/bear-and-steady-gene
import sys
N = int(raw_input())
S = list(raw_input())
def substring_len(s):
#Get count of letters
letter_count = {'A':0,'T':0,'G':0,'C':0}
for i in range(len(s)):
letter_count[s[i]] += 1
for k,v in letter_count.items():
if v > N/4:
letter_count[k] = int(letter_count[k] - N/4)
else:
letter_count[k] = 0
change_count = 0
for v in letter_count.values():
change_count += v
if change_count == 0:
return 0
min_length = len(s)
start = 0
end = 0
hasFound = False
substring_count = {'A':0,'T':0,'G':0,'C':0}
while True:
if end >= len(s):
return min_length
hasFound = True
for k,v in substring_count.items():
if substring_count[k] < letter_count[k]:
hasFound = False
if hasFound == True:
if end - start < min_length:
min_length = end - start
substring_count[s[start]] -= 1
start += 1
continue
if hasFound == False:
substring_count[s[end]] += 1
end += 1
continue
print(substring_len(S))