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

PythonServer Side ProgrammingProgramming

Suppose we have a string s. We have to check whether the vowels present in s are in alphabetical order or not.

So, if the input is like s = "helloyou", then the output will be True as vowels are e, o, o, u all are in alphabetical order.

To solve this, we will follow these steps −

  • character := character whose ASCII is 64
  • for i in range 0 to size of s - 1, do
    • if s[i] is any one of ('A','E','I','O','U','a','e','i','o','u'), then
      • if s[i] < character, then
        • return False
      • otherwise,
        • character := s[i]
  • return True

Let us see the following implementation to get better understanding −

Example Code

Live Demo

def solve(s):
   character = chr(64)

   for i in range(len(s)):
      if s[i] in ['A','E','I','O','U','a','e','i','o','u']:
         if s[i] < character:
           return False
         else:
           character = s[i]
 
   return True

s = "helloyou"
print(solve(s))

Input

"helloyou"

Output

True
raja
Published on 16-Jan-2021 05:05:30
Advertisements