-
Notifications
You must be signed in to change notification settings - Fork 193
/
substring_two_chars.py
72 lines (59 loc) · 1.94 KB
/
substring_two_chars.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'''
Question statement:
Return the longest contiguous substring of 2 distinct characters from an
input string.
Example
input: abbaacab
output : abbaa
input: abcefabbabaabefghghfa
return: abbabaab
input: aabceddddcdccecabceftg
return: ddddcdcc
input: acbabbcbca
return : bbcbc
'''
def max_contiguous(string):
if len(string) <= 2:
return string
curr_max_str = curr_blob = string[:2]
char0, char1 = string[0], string[1]
last_char = char1
if char0 == char1:
last_contiguous_idx = 0
last_last_char = None
else:
last_contiguous_idx = 1
last_last_char = char0
for i, char in enumerate(string[2:]):
if last_last_char is None:
curr_blob += char
if char != last_char:
last_contiguous_idx = i + 2
last_last_char = last_char
last_char = char
else:
if char == last_char:
curr_blob += char
elif char == last_last_char:
curr_blob += char
last_contiguous_idx = i + 2
last_last_char = last_char
last_char = char
else:
curr_blob = string[last_contiguous_idx: i + 3]
last_contiguous_idx = i + 2
last_last_char = last_char
last_char = char
if len(curr_blob) > len(curr_max_str):
curr_max_str = curr_blob[:]
return curr_max_str
assert max_contiguous('abbaacab') == 'abbaa'
assert max_contiguous('abcefabbabaabefghghfa') == 'abbabaab'
assert max_contiguous('aabceddddcdccecabceftg') == 'ddddcdcc'
assert max_contiguous('acbabbcbca') == 'bbcbc'
assert max_contiguous('') == ''
assert max_contiguous('aaaaaaaa') == 'aaaaaaaa'
assert max_contiguous(
'aaaaabbbbb3dasfa938209320202020202020202') == '20202020202020202'
assert max_contiguous(
'aaaaabbbbb3dasfa938209320202020202020202afdafsfasdweeweeeeee') == '20202020202020202'