[Python3] greedy
\n\nclass Solution:\n    def longestBeautifulSubstring(self, word: str) -> int:\n        vowels = "aeiou"\n        ans = 0\n        cnt = prev = -1 \n        fo
\n```\nclass Solution:\n    def longestBeautifulSubstring(self, word: str) -> int:\n        vowels = "aeiou"\n        ans = 0\n        cnt = prev = -1 \n        for i, x in enumerate(word): \n            curr = vowels.index(x)\n            if cnt >= 0: # in the middle of counting \n                if 0 <= curr - prev <= 1: \n                    cnt += 1\n                    if x == "u": ans = max(ans, cnt)\n                elif x == "a": cnt = 1\n                else: cnt = -1 \n            elif x == "a": cnt = 1\n            prev = curr \n        return ans \n```\n\nAlternative implementations\n```\nclass Solution:\n    def longestBeautifulSubstring(self, word: str) -> int:\n        ans = 0\n        cnt = unique = 1\n        for i in range(1, len(word)): \n            if word[i-1] <= word[i]: \n                cnt += 1\n                if word[i-1] < word[i]: unique += 1\n            else: cnt = unique = 1\n            if unique == 5: ans = max(ans, cnt)\n        return ans \n```\n\n```\nclass Solution:\n    def longestBeautifulSubstring(self, word: str) -> int:\n        ans = ii = 0\n        unique = 1\n        for i in range(1, len(word)): \n            if word[i-1] > word[i]: \n                ii = i \n                unique = 1\n            elif word[i-1] < word[i]: unique += 1\n            if unique == 5: ans = max(ans, i-ii+1)\n        return ans \n```
['Python3']