Straightforward Python3 O(n) stack solution with explanations
\nclass Solution:\n    def longestBeautifulSubstring(self, word: str) -> int:\n        d = {}\n        d[\'a\'] = {\'a\', \'e\'}\n        d[\'e\'] = {\'e\', \'i
```\nclass Solution:\n    def longestBeautifulSubstring(self, word: str) -> int:\n        d = {}\n        d[\'a\'] = {\'a\', \'e\'}\n        d[\'e\'] = {\'e\', \'i\'}\n        d[\'i\'] = {\'i\', \'o\'}\n        d[\'o\'] = {\'o\', \'u\'}\n        d[\'u\'] = {\'u\'}\n\t\t\n        res, stack = 0, []\n        for c in word: \n            # If stack is empty, the first char must be \'a\'\n            if len(stack) == 0:\n                if c == \'a\':\n                    stack.append(c)\n                continue \n            \n            # If stack is NOT empty,\n            # input char should be the same or subsequent to the last char in stack\n            # e.g., last char in stack is \'a\', next char should be \'a\' or \'e\'\n            # e.g., last char in stack is \'e\', next char should be \'e\' or \'i\'\n            # ...\n            # e.g., last char in stack is \'u\', next char should be \'u\'\n            if c in d[stack[-1]]:\n                stack.append(c)\n                # If the last char in stack is eventually \'u\', \n                # then we have one beautiful substring as candidate, \n                # where we record and update max length of beautiful substring (res)\n                if c == \'u\':\n                    res = max(res, len(stack))\n            else:\n                stack = [] if c != \'a\' else [\'a\']\n                \n        return res\n```
['Stack', 'Python3']