Created
June 6, 2018 20:19
-
-
Save iAbadia/0d2bf30bfd875fedd49ef20456d1fc52 to your computer and use it in GitHub Desktop.
Magic vowels python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vowel = "aeiou" | |
def vpos(c): | |
for i in range(5): | |
if c == vowel[i]: | |
return i | |
return -1 | |
def magical(s): | |
l = len(s) | |
previndex = [-1, -1, -1, -1, -1] | |
leng = [0] * l | |
ii = 0 | |
maxlen = 0 | |
while (s[ii] != 'a'): | |
ii += 1 | |
if ii == l: | |
return 0 | |
previndex[0] = ii | |
leng[ii] = 1 | |
for i in range(ii + 1, l): | |
if previndex[vpos(s[i])] >= 0: | |
leng[i] = 1+leng[previndex[vpos(s[i])]] | |
previndex[vpos(s[i])] = i | |
if s[i] != 'a': | |
if previndex[vpos(s[i])-1] >= 0: | |
leng[i] = max(leng[i], 1+leng[previndex[vpos(s[i])-1]]) | |
maxlen = max(maxlen, leng[i]) | |
return maxlen | |
def main(): | |
s = "aeiaaioooaauuaeiou" | |
print(s) | |
print(magical(s)) | |
return 0 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment