Faster than 93% of C# submissions | Inline comments | O(N) time
\npublic class Solution {\n    public int LongestBeautifulSubstring(string str) {\n        int max = 0, s = 0;\n        char last = \'$\';\n\t\t// Store vowel s
```\npublic class Solution {\n    public int LongestBeautifulSubstring(string str) {\n        int max = 0, s = 0;\n        char last = \'$\';\n\t\t// Store vowel sequence\n        var map = new Dictionary<char, int>() {\n            {\'a\',0}, {\'e\',1},{\'i\',2},{\'o\',3},{\'u\',4}\n        };\n        for(int i=0;i<str.Length;i++)\n        {\n            if(last==\'$\') //No beautiful string in getting processed\n            {\n                if(str[i]!=\'a\') continue; //beautiful string starts with \'a\'\n                s = i;  // mark the start index, used later for calculating length\n                while(i<str.Length-1 && str[i+1]==\'a\') {\n                    i++;   \n                }\n                last = \'a\';\n            }\n            else {\n                char cur = str[i];\n                if((map[last]+1)!=map[cur]) { // not in sequence\n                    if(cur==\'a\') i--; // if it is \'a\', it can be a beautiful string, so consider it again\n                    last = \'$\';\n                    s = 0;\n                    continue;\n                }\n                else if((map[last]+1)==map[str[i]]) {\n                    while(i<str.Length-1 && str[i+1]==cur) {\n                        i++;     \n                    }\n                    if(cur==\'u\') { // Last char of a beautiful string, find the length\n                        max = Math.Max(max,i-s+1);\n\t\t\t\t\t\t // re-initialize for next beautiful strings, if any:\n                        last = \'$\';\n                        s = 0;\n                    }\n                    else last = cur;\n                }\n            }\n        }\n        return max;\n    }\n}\n```\nPlease upvote if it helps!
[]