c++ - O(n) time and O(1) space, beats 93% | with comments
\nclass Solution {\npublic:\n    // concept used is sliding window.\n    // O(n) time complexity and O(1) space complexity.\n    int longestBeautifulSubstring(s
```\nclass Solution {\npublic:\n    // concept used is sliding window.\n    // O(n) time complexity and O(1) space complexity.\n    int longestBeautifulSubstring(string word) {\n        int maxlen=0;      // this will store the max length of valid substring\n        int start=0,end=0; // the pointers to traverse the sliding window\n        int n=word.size();\n       \n        while(end<n && word[end]!=\'a\')\n            end++;           // starting from the point where first occurence of \'a\' is there\n       \n        start=end;\n        int prev=0; // this will traverse the vowels vector \n        vector<char>vowels{\'a\',\'e\',\'i\',\'o\',\'u\'};\n       \n        while(end<n)\n        {\n            \n            if(word[end]>=vowels[prev])\n            {\n                if(prev==4)      // prev==4 means we have reached the end of vowels, and now we should update the maxlen if possible.\n                    maxlen=max(maxlen,end-start+1);\n                else if(word[end]==vowels[prev+1])   // this tells us that vowels have come in order i.e \'a\' then \'e\' then \'i\' then \'o\' and then \'u\', so there is no harm in incrementing prev. \n                {\n                    prev++;\n                    if(prev==4)\n                        maxlen=max(maxlen,end-start+1);\n                }\n                else if(word[end]>vowels[prev+1])   // this situation is not good for us as the consecutive order of vowels have broken, so we have to reset everything and make start and end to the position where the next \'a\' occurs, if it is there.\n                {\n                    prev=0;\n                    while(end<n && word[end]!=\'a\')\n                    {\n                        end++;\n                    }\n                    start=end;\n                    end--;\n                }\n            }\n           \n            else   // this is also the situation where the consecutive order breaks, so again reset\n            {\n                prev=0;\n                while(end<n && word[end]!=\'a\')\n                    end++;\n                start=end;\n                end--;\n            }\n          \n            end++;\n        }\n        return maxlen;\n    }\n};\n```
['Two Pointers', 'C', 'Sliding Window', 'C++']