Program to sort all vowels at beginning then the consonants, are in sorted order in Python

Suppose we have a lowercase alphabet string s, we have to find a string with all the vowels of s in sorted sequence followed by all consonants of s in sorted sequence.

So, if the input is like "helloworld", then the output will be "eoodhlllrw", as vowels are "eo" And consonants are in sorted order "dhlllrw"

To solve this, we will follow these steps −

  • k := blank string, t := blank string
  • for each character c in s, do
    • if c is a vowel, then
      • k := k concatenate c
    • otherwise,
      • t := t concatenate c
  • return (k after sort and concatenate t after sorting)

Let us see the following implementation to get better understanding −


 Live Demo

class Solution:
   def solve(self, s):
      vowels = 'aeiou' k = ''
      t = ''
      for c in s:
         if c in vowels :
            k = k + c
         else :
            t = t + c
            k = ''.join(sorted(k))
            t = ''.join(sorted(t))
      return k + t
ob = Solution() print(ob.solve("helloworld"))





Updated on: 05-Oct-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started