two pointers
cpp\n// use this to simplify the comparison between vowels\nmap<char, int> idx = {\n\t{\'a\', 0}, \n\t{\'e\', 1},\n\t{\'i\', 2},\n\t{\'o\', 3},\n\t{\'u\', 4}\n}
```cpp\n// use this to simplify the comparison between vowels\nmap<char, int> idx = {\n\t{\'a\', 0}, \n\t{\'e\', 1},\n\t{\'i\', 2},\n\t{\'o\', 3},\n\t{\'u\', 4}\n};\n\nint longestBeautifulSubstring(string word) {\n\tint ret = 0;\n\t\n\t// for each char in word,\n\tfor (int i = 0; i < word.size(); ++i)\n\t{\n\t\t// we only consider possible start of substring, which should be \'a\',\n\t\tif (word[i] == \'a\')\n\t\t{\n\t\t\tint j = i + 1;\n\t\t\t// record the max vowel in this substring,\n\t\t\tchar ma = \'a\';\n\t\t\t\n\t\t\t// keep growing the current substring,\n\t\t\twhile (j < word.size())\n\t\t\t{\n\t\t\t\t// meet a same char, do nothing and keep growing\n\t\t\t\tif (ma == word[j])\n\t\t\t\t\t;\n\t\t\t\t// meet a next vowel, increase the ma and keep growing\n\t\t\t\telse if (idx[ma] + 1 == idx[word[j]])\n\t\t\t\t\tma = word[j];\n\t\t\t\t// invalid\n\t\t\t\telse\n\t\t\t\t\tbreak;\n\t\t\t\t\n\t\t\t\t++ j;\n\t\t\t}\n\t\t\t\n\t\t\t// meet 2 criteria, record the max length\n\t\t\tif (ma == \'u\')\n\t\t\t\tret = max(ret, j - i);\n\t\t\t\n\t\t\t// jump to the end of this substring, make no sense to consider these chars.\n\t\t\ti = j - 1;\n\t\t}\n\t}\n\t\n\treturn ret;\n}\n```\n
['Two Pointers', 'C']