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++
#include <bits/stdc++.h>
using namespace std;
bool areVowelsInOrder(string s)
{
int n = s.length();
char c = ( char )64;
for ( int i = 0; i < n; i++) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
|| s[i] == 'o' || s[i] == 'u' ) {
if (s[i] < c)
return false ;
else {
c = s[i];
}
}
}
return true ;
}
int main()
{
string s = "aabbbddeecc" ;
if (areVowelsInOrder(s))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean areVowelsInOrder(String s)
{
int n = s.length();
char c = ( char ) 64 ;
for ( int i = 0 ; 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 (s.charAt(i) < c)
return false ;
else {
c = s.charAt(i);
}
}
}
return true ;
}
public static void main(String[] args)
{
String s = "aabbbddeecc" ;
if (areVowelsInOrder(s))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python 3
def areVowelsInOrder(s):
n = len (s)
c = chr ( 64 )
for i in range ( 0 , n):
if (s[i] = = 'a' or s[i] = = 'e' or s[i] = =
'i' or s[i] = = 'o' or s[i] = = 'u' ):
if s[i] < c:
return False
else :
c = s[i]
return True
if __name__ = = "__main__" :
s = "aabbbddeecc"
if areVowelsInOrder(s):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
public static bool areVowelsInOrder( string s)
{
int n = s.Length;
char c = ( char )64;
for ( int i = 0; i < n; i++) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
|| s[i] == 'o' || s[i] == 'u' ) {
if (s[i] < c) {
return false ;
}
else {
c = s[i];
}
}
}
return true ;
}
public static void Main( string [] args)
{
string s = "aabbbddeecc" ;
if (areVowelsInOrder(s)) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
|
PHP
<?php
function areVowelsInOrder( $s )
{
$n = strlen ( $s );
$c = chr (64);
for ( $i = 0; $i < $n ; $i ++)
{
if ( $s [ $i ] == 'a' || $s [ $i ] == 'e' ||
$s [ $i ] == '$i' || $s [ $i ] == 'o' ||
$s [ $i ] == 'u' )
{
if ( $s [ $i ] < $c )
return false;
else {
$c = $s [ $i ];
}
}
}
return true;
}
$s = "aabbbddeecc" ;
if (areVowelsInOrder( $s ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function areVowelsInOrder(s)
{
var n = s.length;
var c = String.fromCharCode(64);
for ( var i = 0; i < n; i++) {
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i'
|| s[i] == 'o' || s[i] == 'u' ) {
if (s[i] < c)
return false ;
else {
c = s[i];
}
}
}
return true ;
}
var s = "aabbbddeecc" ;
if (areVowelsInOrder(s))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
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:
C++
#include <iostream>
#include <stack>
#include <string>
using namespace std;
bool areVowelsInOrder(string s) {
stack< char > vowelStack;
for ( char c : s) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ) {
if (!vowelStack.empty() && c < vowelStack.top()) {
return false ;
}
vowelStack.push(c);
}
}
return true ;
}
int main() {
string s = "aabbbddeecc" ;
if (areVowelsInOrder(s)) {
cout << "Yes\n" ;
}
else {
cout << "No\n" ;
}
return 0;
}
|
Java
import java.util.*;
public class VowelOrderChecker {
static boolean areVowelsInOrder(String s)
{
Stack<Character> vowelStack
= new Stack<Character>();
for ( char c : s.toCharArray()) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o'
|| c == 'u' ) {
if (!vowelStack.empty()
&& c < vowelStack.peek()) {
return false ;
}
vowelStack.push(c);
}
}
return true ;
}
public static void main(String[] args)
{
String s = "aabbbddeecc" ;
if (areVowelsInOrder(s)) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
def are_vowels_in_order(s):
vowel_stack = []
for c in s:
if c in [ 'a' , 'e' , 'i' , 'o' , 'u' ]:
if vowel_stack and c < vowel_stack[ - 1 ]:
return False
vowel_stack.append(c)
return True
s = "aabbbddeecc"
if are_vowels_in_order(s):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class Program
{
static bool AreVowelsInOrder( string s)
{
Stack< char > vowelStack = new Stack< char >();
foreach ( char c in s)
{
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
{
if (vowelStack.Count > 0 && c < vowelStack.Peek())
{
return false ;
}
vowelStack.Push(c);
}
}
return true ;
}
static void Main( string [] args)
{
string s = "aabbbddeecc" ;
if (AreVowelsInOrder(s))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function areVowelsInOrder(s) {
let vowelStack = [];
for (let c of s) {
if ([ 'a' , 'e' , 'i' , 'o' , 'u' ].includes(c)) {
if (vowelStack.length && c < vowelStack[vowelStack.length - 1]) {
return false ;
}
vowelStack.push(c);
}
}
return true ;
}
let s = "aabbbddeecc" ;
if (areVowelsInOrder(s)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Output
Yes
Complexity Analysis:
Time Complexity: O(N), where N is the size of the string
Auxiliary Space: O(N) For Stacks