Longest Beautiful Substring | C++ Solution | Two Pointer + Greedy | O(N) Time
IntuitionWe’re asked to find the length of the longest "beautiful" substring—a substring that:

Contains all 5 vowels: 'a', 'e', 'i', 'o', 'u'


Appears in lexi
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We’re asked to find the length of the longest "beautiful" substring—a substring that:

- Contains all 5 vowels: 'a', 'e', 'i', 'o', 'u'

- Appears in lexicographical order (i.e., 'a' before 'e', 'e' before 'i', etc.)

Instead of checking all substrings (which would be slow), we scan the string once using a greedy + two pointer approach and track progress through the vowel stages.

# Approach
<!-- Describe your approach to solving the problem. -->
Initialize:

- vowelStage = 1: Tracks how many vowels we've encountered in correct order

- left = 0: Start of the current valid substring

- maxLength = 0: Stores the length of the longest beautiful substring

Traverse the string from index 1 to end:

- If the current character is smaller than the previous (order break):

    - Reset vowelStage = 1, and left = i to restart tracking a new possible substring

- If the current character is greater than the previous:

    - Move to the next vowel stage (vowelStage++)

- If vowelStage == 5 (all vowels in order found):

    - Update maxLength with the length of the current window

Return maxLength at the end

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
    O(N)
Single pass through the string of length N

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    O(1)
No extra space used other than a few variables

# Code
``` C++ []
class Solution {
public:
    int longestBeautifulSubstring(string word) {
        int maxLength = 0, left = 0, vowelStage = 1;

        for(int i=1; i<word.size(); i++)
        {
            if(word[i] < word[i-1])
            {
                vowelStage = 1;
                left = i;
            }

            else if(word[i-1] < word[i])
            {
                vowelStage++;
            }

            if(vowelStage == 5)
            {
                maxLength = max(maxLength, i - left + 1);
            }
        }
        return maxLength;
    }
};
```
```java []
class Solution {
    public int longestBeautifulSubstring(String word) {
        int maxLength = 0, vowelStage = 1, left = 0;

        for(int i=1; i<word.length(); i++)
        {
            if(word.charAt(i-1) > word.charAt(i))
            {
                vowelStage = 1;
                left = i;
            }

            else if(word.charAt(i-1) < word.charAt(i))
            {
                vowelStage++;
            }

            if(vowelStage == 5)
            {
                maxLength = Math.max(maxLength, i-left+1);
            }
        
        }
        return maxLength;
    }
}
```

<strong>👍 If this helped, an upvote would be appreciated!

<br>

![upvote (1080 x 1080 px) (400 x 400 px) (400 x 400 px).jpg](https://assets.leetcode.com/users/images/32dee7ff-2eb7-483e-b6a7-e68763a42781_1743960958.610285.jpeg)






['String', 'Sliding Window', 'C++', 'Java']