Alternate vowel and consonant string - GeeksforGeeks

10 Mar, 2022
Given a string, rearrange characters of the given string such that the vowels and consonants occupy alternate position. If string can not be rearranged in desired way, print “no such string”. The order of vowels with respect to each other and the order of consonants with respect to each other should be maintained. If more than one required strings can be formed, print the lexicographically smaller.Examples:  
Input : geeks
Output : gekes

Input : onse
Output : nose
There are two possible outcomes
"nose" and "ones".  Since "nose"
is lexicographically smaller, we 
print it.
 
 
Count number of vowels and consonants in given string.If difference between counts is more than one, return “Not Possible”.If there are more vowels than consonants, print first vowel first and recur for remaining string.If there are more consonants than vowels, print first consonant first and recur for remaining string.If counts are same, compare first vowel with first consonant and print the smaller one first.
Count number of vowels and consonants in given string.
If difference between counts is more than one, return “Not Possible”.
If there are more vowels than consonants, print first vowel first and recur for remaining string.
If there are more consonants than vowels, print first consonant first and recur for remaining string.
If counts are same, compare first vowel with first consonant and print the smaller one first.
 
C++
Java
Python 3
C#
Javascript
// C++ implementation of alternate vowel and// consonant string#include <bits/stdc++.h>using namespace std; // 'ch' is vowel or notbool isVowel(char ch){    if (ch == 'a' || ch == 'e' || ch == 'i' ||            ch == 'o' || ch =='u')        return true;    return false;} // create alternate vowel and consonant string// str1[0...l1-1] and str2[start...l2-1]string createAltStr(string str1, string str2,                    int start, int l){    string finalStr = "";     // first adding character of vowel/consonant    // then adding character of consonant/vowel    for (int i=0, j=start; j<l; i++, j++)        finalStr = (finalStr + str1.at(i)) + str2.at(j);    return finalStr;} // function to find the required// alternate vowel and consonant stringstring findAltStr(string str){    int nv = 0, nc = 0;    string vstr = "", cstr = "";    int l = str.size();    for (int i=0; i<l; i++)    {        char ch = str.at(i);         // count vowels and update vowel string        if (isVowel(ch))        {            nv++;            vstr = vstr + ch;        }         // count consonants and update consonant        // string        else        {            nc++;            cstr = cstr + ch;        }    }     // no such string can be formed    if (abs(nv-nc) >= 2)        return "no such string";     // remove first character of vowel string    // then create alternate string with    // cstr[0...nc-1] and vstr[1...nv-1]    if (nv > nc)        return (vstr.at(0) + createAltStr(cstr, vstr, 1, nv));     // remove first character of consonant string    // then create alternate string with    // vstr[0...nv-1] and cstr[1...nc-1]    if (nc > nv)        return (cstr.at(0) + createAltStr(vstr, cstr, 1, nc));     // if both vowel and consonant    // strings are of equal length    // start creating string with consonant    if (cstr.at(0) < vstr.at(0))        return createAltStr(cstr, vstr, 0, nv);     // start creating string with vowel    return createAltStr(vstr, cstr, 0, nc);} // Driver program to test aboveint main(){    string str = "geeks";    cout << findAltStr(str);    return 0;}
// Java implementation of alternate vowel and// consonant stringimport java.util.*; class GFG{ // 'ch' is vowel or notstatic boolean isVowel(char ch){    if (ch == 'a' || ch == 'e' || ch == 'i' ||            ch == 'o' || ch =='u')        return true;    return false;} // create alternate vowel and consonant string// str1[0...l1-1] and str2[start...l2-1]static String createAltStr(String str1, String str2,                    int start, int l){    String finalStr = "";     // first adding character of vowel/consonant    // then adding character of consonant/vowel    for (int i = 0, j = start; j < l; i++, j++)        finalStr = (finalStr + str1.charAt(i)) +                                    str2.charAt(j);    return finalStr;} // function to find the required// alternate vowel and consonant stringstatic String findAltStr(String str){    int nv = 0, nc = 0;    String vstr = "", cstr = "";    int l = str.length();    for (int i = 0; i < l; i++)    {        char ch = str.charAt(i);         // count vowels and update vowel string        if (isVowel(ch))        {            nv++;            vstr = vstr + ch;        }         // count consonants and update consonant        // string        else        {            nc++;            cstr = cstr + ch;        }    }     // no such string can be formed    if (Math.abs(nv - nc) >= 2)        return "no such string";     // remove first character of vowel string    // then create alternate string with    // cstr[0...nc-1] and vstr[1...nv-1]    if (nv > nc)        return (vstr.charAt(0) + createAltStr(cstr, vstr, 1, nv));     // remove first character of consonant string    // then create alternate string with    // vstr[0...nv-1] and cstr[1...nc-1]    if (nc > nv)        return (cstr.charAt(0) + createAltStr(vstr, cstr, 1, nc));     // if both vowel and consonant    // strings are of equal length    // start creating string with consonant    if (cstr.charAt(0) < vstr.charAt(0))        return createAltStr(cstr, vstr, 0, nv);     // start creating string with vowel    return createAltStr(vstr, cstr, 0, nc);} // Driver codepublic static void main(String args[]){    String str = "geeks";    System.out.println(findAltStr(str));}} // This code is contributed by// Shashank_Sharma
# Python implementation of alternate vowel# and consonant string # 'ch' is vowel or notdef isVowel(ch):    if(ch == 'a' or ch == 'e' or       ch == 'i' or ch == 'o' or       ch == 'u'):        return True    return False # create alternate vowel and consonant string# str1[0...l1-1] and str2[start...l2-1]def createAltStr(str1, str2, start, l):    finalStr = ""    i = 0         # first adding character of vowel/consonant    # then adding character of consonant/vowel    for j in range(start, l):        finalStr = (finalStr + str1[i]) + str2[j]        i + 1     return finalStr # function to find the required# alternate vowel and consonant stringdef findAltStr(str1):    nv = 0    nc = 0    vstr = ""    cstr = ""    l = len(str1)    for i in range(0, l):                 # count vowels and update vowel string        if(isVowel(str1[i])):            nv += 1            vstr = vstr + str1[i]                     # count consonants and update        # consonant string        else:            nc += 1            cstr = cstr + str1[i]                 # no such string can be formed    if(abs(nv - nc) >= 2):        return "no such string"         # remove first character of vowel string    # then create alternate string with    # cstr[0...nc-1] and vstr[1...nv-1]    if(nv > nc):        return (vstr[0] + createAltStr(cstr, vstr, 1, nv))         # remove first character of consonant string    # then create alternate string with    # vstr[0...nv-1] and cstr[1...nc-1]    if(nc > nv):        return (cstr[0] + createAltStr(vstr, cstr, 1, nc))         # if both vowel and consonant    # strings are of equal length    # start creating string with consonant    if(cstr[0] < vstr[0]):        return createAltStr(cstr, vstr, 0, nv)     return createAltStr(vstr, cstr, 0, nc)         # Driver Codeif __name__ == "__main__":    str1 = "geeks"    print(findAltStr(str1)) # This code is contributed by Sairahul099
// C# implementation of alternate vowel and// consonant stringusing System; class GFG{ // 'ch' is vowel or notstatic Boolean isVowel(char ch){    if (ch == 'a' || ch == 'e' || ch == 'i' ||            ch == 'o' || ch =='u')        return true;    return false;} // create alternate vowel and consonant string// str1[0...l1-1] and str2[start...l2-1]static String createAltStr(String str1, String str2,                    int start, int l){    String finalStr = "";     // first adding character of vowel/consonant    // then adding character of consonant/vowel    for (int i = 0, j = start; j < l; i++, j++)        finalStr = (finalStr + str1[i]) +                                    str2[j];    return finalStr;} // function to find the required// alternate vowel and consonant stringstatic String findAltStr(String str){    int nv = 0, nc = 0;    String vstr = "", cstr = "";    int l = str.Length;    for (int i = 0; i < l; i++)    {        char ch = str[i];         // count vowels and update vowel string        if (isVowel(ch))        {            nv++;            vstr = vstr + ch;        }         // count consonants and update consonant        // string        else        {            nc++;            cstr = cstr + ch;        }    }     // no such string can be formed    if (Math.Abs(nv - nc) >= 2)        return "no such string";     // remove first character of vowel string    // then create alternate string with    // cstr[0...nc-1] and vstr[1...nv-1]    if (nv > nc)        return (vstr[0] + createAltStr(cstr, vstr, 1, nv));     // remove first character of consonant string    // then create alternate string with    // vstr[0...nv-1] and cstr[1...nc-1]    if (nc > nv)        return (cstr[0] + createAltStr(vstr, cstr, 1, nc));     // if both vowel and consonant    // strings are of equal length    // start creating string with consonant    if (cstr[0] < vstr[0])        return createAltStr(cstr, vstr, 0, nv);     // start creating string with vowel    return createAltStr(vstr, cstr, 0, nc);} // Driver codepublic static void Main(String []args){    String str = "geeks";    Console.WriteLine(findAltStr(str));}} // This code is contributed by Princi Singh
<script> // JavaScript implementation of alternate vowel and// consonant string // 'ch' is vowel or notfunction isVowel(ch){    if (ch == 'a' || ch == 'e' || ch == 'i' ||            ch == 'o' || ch =='u')        return true;    return false;} // create alternate vowel and consonant string// str1[0...l1-1] and str2[start...l2-1]function createAltStr(str1, str2,start,l){    let finalStr = "";     // first adding character of vowel/consonant    // then adding character of consonant/vowel    for (let i=0, j=start; j<l; i++, j++)        finalStr = (finalStr + str1[i] + str2[j]);    return finalStr;} // function to find the required// alternate vowel and consonant stringfunction findAltStr(str){    let nv = 0, nc = 0;    let vstr = "", cstr = "";    let l = str.length;    for (let i=0; i<l; i++)    {        let ch = str[i];         // count vowels and update vowel string        if (isVowel(ch))        {            nv++;            vstr = vstr + ch;        }         // count consonants and update consonant        // string        else        {            nc++;            cstr = cstr + ch;        }    }     // no such string can be formed    if (Math.abs(nv-nc) >= 2)        return "no such string";     // remove first character of vowel string    // then create alternate string with    // cstr[0...nc-1] and vstr[1...nv-1]    if (nv > nc)        return (vstr[0] + createAltStr(cstr, vstr, 1, nv));     // remove first character of consonant string    // then create alternate string with    // vstr[0...nv-1] and cstr[1...nc-1]    if (nc > nv)        return (cstr[0] + createAltStr(vstr, cstr, 1, nc));     // if both vowel and consonant    // strings are of equal length    // start creating string with consonant    if (cstr.at(0) < vstr.at(0))        return createAltStr(cstr, vstr, 0, nv);     // start creating string with vowel    return createAltStr(vstr, cstr, 0, nc);} // Driver program to test abovelet str = "geeks";document.write(findAltStr(str)); // This code is contributed by Shinjan_Patra</script>
Output:  
gekes
Time Complexity: O(n), where ‘n’ the is length of the stringThis article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sairahul Jella
Shashank_Sharma
princi singh
bunnyram19
shinjanpatra
vowel-consonant
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Different methods to reverse a string in C/C++
Convert string to char array in C++
Array of Strings in C++ (5 Different Ways to Create)
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Length of the longest substring without repeating characters