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

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that checks whether the vowel
// characters in a string are
// in alphabetical order or not
bool areVowelsInOrder(string s)
{
    int n = s.length();
  
    // ASCII Value 64 is less than
    // all the alphabets
    // so using it as a default value
    char c = (char)64;
  
    // check if the vowels in
    // the string are sorted or not
    for (int i = 1; i < n; i++) {
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 
             'i' || s[i] == 'o' || s[i] == 'u') {
  
            // if the vowel is smaller
            // than the previous vowel
            if (s[i] < c) 
                return false;         
            else {
  
                // store the vowel
                c = s[i];
            }
        }
    
    return true;
}
  
// Driver code
int main()
{
    string s = "aabbbddeecc";
  
    // check whether the vowel
    // characters in a string are
    // in alphabetical order or not
    if (areVowelsInOrder(s))
    cout << "Yes";
    else
    cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
  
import java.io.*;
  
class GFG {
  
// Function that checks whether the vowel
// characters in a string are
// in alphabetical order or not
static boolean areVowelsInOrder(String s)
{
    int n = s.length();
  
    // ASCII Value 64 is less than
    // all the alphabets
    // so using it as a default value
    char c = (char)64;
  
    // check if the vowels in
    // the string are sorted or not
    for (int i = 1; i < n; i++) {
        if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' 
               || s.charAt(i) == 'o' || s.charAt(i) == 'u') {
  
            // if the vowel is smaller than the previous vowel
            if (s.charAt(i) < c) 
                return false;     
            else {
  
                // store the vowel
                c = s.charAt(i);
            }
        }
    
    return true;
}
  
// Driver code
    public static void main (String[] args) {
            String s = "aabbbddeecc";
  
    // check whether the vowel
    // characters in a string are
    // in alphabetical order or not
    if (areVowelsInOrder(s))
    System.out.print("Yes");
    else
    System.out.print("No");
  
    }
}
  
// This Code is contributed
// by anuj_67....

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of 
# above approach 
  
# Function that checks whether the vowel 
# characters in a string are 
# in alphabetical order or not 
def areVowelsInOrder(s) :
  
    n = len(s)
  
    # ASCII Value 64 is less than 
    # all the alphabets 
    # so using it as a default value 
    c = chr(64)
  
    # check if the vowels in 
    # the string are sorted or not 
    for i in range(1, n) :
          
        if (s[i] == 'a' or s[i] == 'e' or s[i] ==
            'i' or s[i] == 'o' or s[i] == 'u') :
  
            # if the vowel is smaller 
            # than the previous vowel 
            if s[i] < c :
                return False
            else :
                  
                # store the vowel 
                c = s[i]
  
    return True
  
# Driver code     
if __name__ == "__main__" :
  
    s = "aabbbddeecc"
  
    # check whether the vowel 
    # characters in a string are 
    # in alphabetical order or not 
    if areVowelsInOrder(s) :
        print("Yes")
  
    else :
        print("No")
  
# This code is contributed by 
# ANKITRAI1

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach 
using System;
  
class GFG {
  
// Function that checks whether the vowel 
// characters in a string are 
// in alphabetical order or not 
public static bool areVowelsInOrder(string s)
{
    int n = s.Length;
  
    // ASCII Value 64 is less than 
    // all the alphabets 
    // so using it as a default value 
    char c = (char)64;
  
    // check if the vowels in 
    // the string are sorted or not 
    for (int i = 1; i < n; i++)
    {
        if (s[i] == 'a' || s[i] == 'e' || s[i] == 
               'i' || s[i] == 'o' || s[i] == 'u')
        {
  
            // if the vowel is smaller 
            // than the previous vowel 
            if (s[i] < c)
            {
                return false;
            }
            else
            {
  
                // store the vowel 
                c = s[i];
            }
        }
    }
    return true;
}
  
// Driver code 
public static void Main(string[] args)
{
    string s = "aabbbddeecc";
  
    // check whether the vowel 
    // characters in a string are 
    // in alphabetical order or not 
    if (areVowelsInOrder(s))
    {
        Console.Write("Yes");
    }
      
    else
    {
        Console.Write("No");
    }
  
}
}
  
// This code is contributed
// by Shrikant13

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Function that checks whether the vowel
// characters in a string are
// in alphabetical order or not
function areVowelsInOrder($s)
{
    $n = strlen($s);
  
    // ASCII Value 64 is less than
    // all the alphabets
    // so using it as a default value
    $c = chr(64);
  
    // check if the vowels in
    // the string are sorted or not
    for ($i = 1; $i < $n; $i++)
    {
        if ($s[$i] == 'a' || $s[$i] == 'e' || 
            $s[$i] == '$i' || $s[$i] == 'o' || 
            $s[$i] == 'u'
        {
  
            // if the vowel is smaller
            // than the previous vowel
            if ($s[$i] < $c
                return false;     
            else {
  
                // store the vowel
                $c = $s[$i];
            }
        }
    
    return true;
}
  
// Driver code
$s = "aabbbddeecc";
  
// check whether the vowel
// characters in a string are
// in alphabetical order or not
if (areVowelsInOrder($s))
    echo "Yes";
else
    echo "No";
      
// This code is contributed by ihritik
?>

chevron_right


Output:

Yes

Complexity: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.