-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1839longestsubstringofvowels.py
64 lines (58 loc) · 2.35 KB
/
1839longestsubstringofvowels.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
class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
i = 0
vowels = 'aeiou'
vowelindex = 0
maxlength = 0
print('\n' + str(len(word)))
while i < len(word):
letter = word[i]
if letter == vowels[vowelindex]:
# print(f"scan started {i}")
currentlength = 1
complete = False
# countprevious = 1
# print(f"\nfound a")
while vowelindex <= 4 and (i < len(word) - 1):
i += 1
letter = word[i]
if letter == vowels[vowelindex]:
currentlength += 1
# countprevious += 1
if i == len(word) - 1 and vowelindex == 4:
complete = True
elif vowelindex == 4:
# print(f"same {word[i-1]} for {countprevious} times")
# print("end of scan")
# print(f"scan ended {i-1}")
i -= 1
complete = True
break
elif letter == vowels[vowelindex + 1]:
# print(f"same {word[i-1]} for {countprevious} times")
# countprevious = 1
# print(f"next letter {letter}")
currentlength += 1
vowelindex += 1
if i == len(word) - 1 and vowelindex == 4:
complete = True
else:
# print(f"{letter} not after {word[i-1]}")
i -= 1
vowelindex = 0
break
if complete:
vowelindex = 0
maxlength = max(currentlength, maxlength)
# print("complete scan")
# print(f"total {currentlength}")
else:
pass
i += 1
return maxlength
def test_():
testfunc = Solution().longestBeautifulSubstring
assert testfunc("xaeiuadfaaaaeeeeeiiiiouuuu") == 18
assert testfunc("aeeeiiiioooauuuaeiou") == 5
assert testfunc("eoauiauioeioeauuiaoeeuoiauiaoeuaeoiiouaeeiauouioea") == 0
assert testfunc("aaaaa") == 2