C++ Solution using Stack
Below is the solution using stack. I will explain each and every step of my logic\n1. It is obvious that if the length of the string is smaller than 5 the answe
Below is the solution using stack. I will explain each and every step of my logic\n1. It is obvious that if the length of the string is smaller than **5** the answer is **0**.\n2. Initialising all the variables and stack of characters\n```\n\tstack<char> st;\n\tint i=0,j=0,count=0,ans=INT_MIN,f=0;\n```\n3.We run a while loop until `i<word.size();`\n4.Now we know that the first letter of the substring has to be \'a\'.So, we check if stack is empty and `word[i]==\'a\'` then push it into the stack otherwise we just move ahead.The first line inside the block of the loop shows this.\n```\nif(st.empty()&&word[i]!=\'a\')i++;\n```\n5.Now if `word[i]==\'a\'` and stack is empty or `st.top()==\'a\'` we push `word[i]` in the stack and increase the count by 1.\n6.We have to keep in mind that if `word[i]==\'a\'` then `st.top()==\'a\'||st.empty()`.\n    If this is not the case then we pop out each and every element from the stack and repeat the process again.\n7.Simarily for `word[i]==\'e\'` `st.top()==\'a\'||st.top==\'e\'` and `word[i]==\'i\'` `st.top()==\'e\'||st.top()==\'i\'`and so on.\n8. When we reach `word[i]==\'u\'`. `if(st.top()==\'o\'||st.top()==\'u\')` we increase the count by 1 and `ans = max(ans,count)`.Also one has to keep in mind that substring should contain atleast one occurrence of each and every vowel in the substring .To check if this we use a flag variable `f` whose initial value is zero.When it reaches the above mentioned if block we make `f=1`.\n9. Out of the block of loop we check `if(f==0)`.If it is true then the answer is zero as there is no occurrence of u in the substring.Else the answer becomes answer.\nThe entire code is given below:\n```\nclass Solution {\npublic:\n    int longestBeautifulSubstring(string word) {\n        if(word.size()<5)return 0;\n        stack<char> st;\n        int i=0,j=0,count=0,ans=INT_MIN,f=0;//here f is the flag variable\n        while(i<word.size()){\n            if(st.empty()&&word[i]!=\'a\')i++;//checks if the first letter of the substring is \'a\'\n            else if(word[i]==\'a\'){\n                if(st.empty()||st.top()==\'a\'){\n                    st.push(word[i]);\n                    count++;\n                    i++;\n                }\n                else{\n                    while(!st.empty()){\n                        st.pop();\n                    }\n                    count=0;\n                }\n            }\n            else if(word[i]==\'e\'){\n                if(st.top()==\'a\'||st.top()==\'e\'){\n                    st.push(word[i]);\n                    count++;\n                    ans=max(ans,count);\n                    i++;\n                }\n                else{\n                    while(!st.empty()){\n                        st.pop();\n                    }\n                    count=0;\n                }\n            }\n            else if(word[i]==\'i\'){\n                if(st.top()==\'e\'||st.top()==\'i\'){\n                    st.push(word[i]);\n                    count++;\n                    i++;\n                }\n                else{\n                    while(!st.empty()){\n                        st.pop();\n                    }\n                    count=0;\n                }\n            }\n            else if(word[i]==\'o\'){\n                if(st.top()==\'i\'||st.top()==\'o\'){\n                    st.push(word[i]);\n                    count++;\n                    i++;\n                }\n                else{\n                    while(!st.empty()){\n                        st.pop();\n                    }\n                    count=0;\n                }\n            }\n            else if(word[i]==\'u\'){\n                if(st.top()==\'o\'||st.top()==\'u\'){\n                    st.push(word[i]);\n                    count++;\n                    ans=max(count,ans);\n                    i++;\n                    f=1;//f becomes 1 if this block is reached conforming that there is an occurence of every vowel in the substring\n                }\n                else{\n                    while(!st.empty()){\n                        st.pop();\n                    }\n                    count=0;\n                }\n            }\n        }\n        if(f==0)return 0;//every vowel doesn\'t appear in the substring so answer is zero\n        return ans;//maximum length of the substring\n    }\n};\n```\nFeel free to check this out.Ignore the grammatical mistakes. Comment if any doubt.\n
[]