VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-the-vowels-in-a-string-are-in-alphabetical-order-or-not/

⇱ Check whether the vowels in a string are in alphabetical order or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check whether the vowels in a string are in alphabetical order or not

Last Updated : 31 Mar, 2023

Given a string 'str', the task is to find whether the vowels in the string are in alphabetical order or not. The string contains only lowercase alphabets.

Examples: 

Input: str = "aabbbddeecc"
Output: Vowels are in alphabetical order
The vowel characters in the string are : a, a, e, e 
which are in sorted order.

Input: str = "aabbbdideecc"
Output: Vowels are not in alphabetical order

Approach: 

  • Traverse the array and find the characters which are vowels.
  • If any vowel is less than the vowel which occurred previously in the array then print Vowels are not in alphabetical order.
  • Else, print Vowels are in alphabetical order.

Below is the implementation of the above approach:  


Output
Yes

Complexity Analysis:

  • Time Complexity: O(N)
  • Auxiliary Space: O(1)

Approach 2: Using Stack:

We can also solve this problem using a stack data structure in C++. We can push the vowel characters encountered onto the stack as we iterate through the string. If we encounter a new vowel character that is smaller than the top of the stack, we know that the vowels are not in alphabetical order. Otherwise, we continue iterating and pushing onto the stack. If we reach the end of the string without finding any violations, we know that the vowels are in alphabetical order.

Here's the code using a stack:

Output

Yes


Complexity Analysis:

Time Complexity: O(N), where N is the size of the string
Auxiliary Space: O(N) For Stacks
 

Comment
Article Tags:
Article Tags: