-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(Failed)Two_Characters.py
72 lines (67 loc) · 2.09 KB
/
(Failed)Two_Characters.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
'''
Finished function:
1.transfer 'beabeefeab' to 'babfab' p28-p44 (find the consecutive characters, and remove them all!!! from the string)
2.if the remained string's set contain 3 distinct characters, then can get the right answer
3.if the remained string's set has more than 3 distinct characers, failed!
'''
#!/bin/python3
import sys
def is_consecutive(s):
mystack = []
for c in s:
if mystack:
if c == mystack[-1]:
return True
else:
mystack.append(c)
else:
mystack.append(c)
return False
def delete_char(s, char):
res = []
for c in s:
if c != char:
res.append(c)
return res
def twoCharaters(s):
# Complete this function
if len(s) == 0 or len(s) == 1:
return 0
flag = []
mystack = []
len_list = []
for c in s:
if mystack:
if c == mystack[-1]:
flag.append(c)
mystack.pop()
else:
mystack.append(c)
else:
mystack.append(c)
for each in flag:
while each in mystack:
mystack.remove(each)
temp_set = set(mystack)
if len(temp_set) == 2:
return len(mystack)
temp_mystack = mystack[:]
for c in temp_set:
while c in temp_mystack:
temp_mystack.remove(c)
if len(set(temp_mystack)) > 2:
continue
if not is_consecutive(temp_mystack):
len_list.append(len(temp_mystack))
temp_mystack = mystack[:]
if len_list:
return max(len_list)
return 0
if __name__ == "__main__":
#l = int(input().strip())
s = 'beabheefeab'
result = twoCharaters(s)
print(result)
'''
uyetuppelecblwipdsqabzsvyfaezeqhpnalahnpkdbhzjglcuqfjnzpmbwprelbayyzovkhacgrglrdpmvaexkgertilnfooeazvulykxypsxicofnbyivkthovpjzhpohdhuebazlukfhaavfsssuupbyjqdxwwqlicbjirirspqhxomjdzswtsogugmbnslcalcfaxqmionsxdgpkotffycphsewyqvhqcwlufekxwoiudxjixchfqlavjwhaennkmfsdhigyeifnoskjbzgzggsmshdhzagpznkbahixqgrdnmlzogprctjggsujmqzqknvcuvdinesbwpirfasnvfjqceyrkknyvdritcfyowsgfrevzon
'''