Given a string, rearrange the characters of the given string such that the vowels and consonants occupy the alternate position. If the string can not be rearranged in the 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 string 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 the first vowel first and recur for the remaining string.
- If there are more consonants than vowels, print the first consonant first and recur for the remaining string.
- If counts are same, compare first vowel with first consonant and print the smaller one first.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' )
return true ;
return false ;
}
string createAltStr(string str1, string str2,
int start, int l)
{
string finalStr = "" ;
for ( int i=0, j=start; j<l; i++, j++)
finalStr = (finalStr + str1.at(i)) + str2.at(j);
return finalStr;
}
string 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);
if (isVowel(ch))
{
nv++;
vstr = vstr + ch;
}
else
{
nc++;
cstr = cstr + ch;
}
}
if ( abs (nv-nc) >= 2)
return "no such string" ;
if (nv > nc)
return (vstr.at(0) + createAltStr(cstr, vstr, 1, nv));
if (nc > nv)
return (cstr.at(0) + createAltStr(vstr, cstr, 1, nc));
if (cstr.at(0) < vstr.at(0))
return createAltStr(cstr, vstr, 0, nv);
return createAltStr(vstr, cstr, 0, nc);
}
int main()
{
string str = "geeks" ;
cout << findAltStr(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' )
return true ;
return false ;
}
static String createAltStr(String str1, String str2,
int start, int l)
{
String finalStr = "" ;
for ( int i = 0 , j = start; j < l; i++, j++)
finalStr = (finalStr + str1.charAt(i)) +
str2.charAt(j);
return finalStr;
}
static 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);
if (isVowel(ch))
{
nv++;
vstr = vstr + ch;
}
else
{
nc++;
cstr = cstr + ch;
}
}
if (Math.abs(nv - nc) >= 2 )
return "no such string" ;
if (nv > nc)
return (vstr.charAt( 0 ) + createAltStr(cstr, vstr, 1 , nv));
if (nc > nv)
return (cstr.charAt( 0 ) + createAltStr(vstr, cstr, 1 , nc));
if (cstr.charAt( 0 ) < vstr.charAt( 0 ))
return createAltStr(cstr, vstr, 0 , nv);
return createAltStr(vstr, cstr, 0 , nc);
}
public static void main(String args[])
{
String str = "geeks" ;
System.out.println(findAltStr(str));
}
}
|
Python 3
def isVowel(ch):
if (ch = = 'a' or ch = = 'e' or
ch = = 'i' or ch = = 'o' or
ch = = 'u' ):
return True
return False
def createAltStr(str1, str2, start, l):
finalStr = ""
i = 0
for j in range (start, l):
finalStr = (finalStr + str1[i]) + str2[j]
i + 1
return finalStr
def findAltStr(str1):
nv = 0
nc = 0
vstr = ""
cstr = ""
l = len (str1)
for i in range ( 0 , l):
if (isVowel(str1[i])):
nv + = 1
vstr = vstr + str1[i]
else :
nc + = 1
cstr = cstr + str1[i]
if ( abs (nv - nc) > = 2 ):
return "no such string"
if (nv > nc):
return (vstr[ 0 ] + createAltStr(cstr, vstr, 1 , nv))
if (nc > nv):
return (cstr[ 0 ] + createAltStr(vstr, cstr, 1 , nc))
if (cstr[ 0 ] < vstr[ 0 ]):
return createAltStr(cstr, vstr, 0 , nv)
return createAltStr(vstr, cstr, 0 , nc)
if __name__ = = "__main__" :
str1 = "geeks"
print (findAltStr(str1))
|
C#
using System;
class GFG
{
static Boolean isVowel( char ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' )
return true ;
return false ;
}
static String createAltStr(String str1, String str2,
int start, int l)
{
String finalStr = "" ;
for ( int i = 0, j = start; j < l; i++, j++)
finalStr = (finalStr + str1[i]) +
str2[j];
return finalStr;
}
static 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];
if (isVowel(ch))
{
nv++;
vstr = vstr + ch;
}
else
{
nc++;
cstr = cstr + ch;
}
}
if (Math.Abs(nv - nc) >= 2)
return "no such string" ;
if (nv > nc)
return (vstr[0] + createAltStr(cstr, vstr, 1, nv));
if (nc > nv)
return (cstr[0] + createAltStr(vstr, cstr, 1, nc));
if (cstr[0] < vstr[0])
return createAltStr(cstr, vstr, 0, nv);
return createAltStr(vstr, cstr, 0, nc);
}
public static void Main(String []args)
{
String str = "geeks" ;
Console.WriteLine(findAltStr(str));
}
}
|
Javascript
<script>
function isVowel(ch)
{
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u' )
return true ;
return false ;
}
function createAltStr(str1, str2,start,l)
{
let finalStr = "" ;
for (let i=0, j=start; j<l; i++, j++)
finalStr = (finalStr + str1[i] + str2[j]);
return finalStr;
}
function 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];
if (isVowel(ch))
{
nv++;
vstr = vstr + ch;
}
else
{
nc++;
cstr = cstr + ch;
}
}
if (Math.abs(nv-nc) >= 2)
return "no such string" ;
if (nv > nc)
return (vstr[0] + createAltStr(cstr, vstr, 1, nv));
if (nc > nv)
return (cstr[0] + createAltStr(vstr, cstr, 1, nc));
if (cstr.at(0) < vstr.at(0))
return createAltStr(cstr, vstr, 0, nv);
return createAltStr(vstr, cstr, 0, nc);
}
let str = "geeks" ;
document.write(findAltStr(str));
</script>
|
Time Complexity: O(n), where ‘n’ the is length of the string
Auxiliary Space: O(n), where ‘n’ is the length of the string.
By Hashing:
The idea is to use the hash table to store the vowels and consonants occurrence then apply the simple brute force.
Steps to solve the problem:
1. declare the vector mp1 and mp2 to store the occurrence and variable v and c to store the count of vowel and consonants.
2. iterate through the string and increment the occurrence of vowel and consonants in the hash table.
3. if absolute difference of v and c is greater than 1, in this case string with alternate vowel and consonant is not possible so we will return “no such string”.
4. declare variable it1 , it2 and i to iterate through the vector to find first vowel and consonants.
5. while it1<mp1.size() and mp1[it1] is equal to zero:
- we will increment the first iterator.
6. while it2<mp2.size() and mp2[it2] is equal to zero:
- we will increment the second iterator.
7. declare Boolean f to store c is greater than v or not to check either consonants will be at first place or the vowel.
8. if v is equal to c:
- f=it1>it2 (lexicographically smaller)
9. while it1 is smaller than mp1.size() and it2 is smaller than mp2.size() and i is smaller than n:
- if f is true s[i]=it2+’a’ , –mp2[it2] and iterate through mp2 till mp2[it2] is equal to zero and increment it2, f=false.
- else s[i]=it1+’a’ , –mp1[it1] and iterate through mp1 till mp1[it1] is equal to zero and increment it1, f=true.
- increment of i.
10. check for condition where only one vowel or consonant is left.
11. return the string.
Implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
string findAltStr(string s)
{
int n = s.size();
vector< int > mp1(26),
mp2(26);
int v = 0, c = 0;
for ( char ch : s) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u' ) {
mp1[ch - 'a' ]++;
v++;
}
else {
mp2[ch - 'a' ]++;
c++;
}
}
if ( abs (v - c) > 1)
return "no such string" ;
int it1 = 0, it2 = 0, i = 0;
while (it1 < mp1.size() && mp1[it1] == 0)
it1++;
while (it2 < mp2.size() && mp2[it2] == 0)
it2++;
bool f
= c > v;
if (v == c) {
f = it1 > it2;
}
while ((it1 < mp1.size() && it2 < mp2.size())
&& i < n) {
if (f) {
s[i] = it2 + 'a' ;
--mp2[it2];
while (it2 < mp2.size() && mp2[it2] == 0)
it2++;
f = false ;
}
else {
s[i] = it1 + 'a' ;
--mp1[it1];
while (it1 < mp1.size() && mp1[it1] == 0)
it1++;
f = true ;
}
++i;
}
if (it1 != mp1.size())
s[i] = it1 + 'a' ;
else if (it2 != mp2.size())
s[i] = it2 + 'a' ;
return s;
}
int main()
{
string str = "geeks" ;
cout << findAltStr(str);
return 0;
}
|
Java
import java.util.*;
public class GFG {
static String findAltStr(String str)
{
char [] s = str.toCharArray();
int n = s.length;
int [] mp1 = new int [ 26 ];
int [] mp2
= new int [ 26 ];
int v = 0 , c = 0 ;
for ( char ch : s) {
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o'
|| ch == 'u' ) {
mp1[ch - 'a' ]++;
v++;
}
else {
mp2[ch - 'a' ]++;
c++;
}
}
if (Math.abs(v - c) > 1 )
return "no such string" ;
int it1 = 0 , it2 = 0 , i = 0 ;
while (it1 < mp1.length && mp1[it1] == 0 )
it1++;
while (it2 < mp2.length && mp2[it2] == 0 )
it2++;
boolean f = c > v;
if (v == c) {
f = it1
> it2;
}
while ((it1 < mp1.length && it2 < mp2.length)
&& i < n) {
if (f) {
s[i] = ( char )(it2 + 'a' );
--mp2[it2];
while (it2 < mp2.length && mp2[it2] == 0 )
it2++;
f = false ;
}
else {
s[i] = ( char )(it1 + 'a' );
--mp1[it1];
while (it1 < mp1.length && mp1[it1] == 0 )
it1++;
f = true ;
}
++i;
}
if (it1 != mp1.length)
s[i] = ( char )(it1 + 'a' );
else if (it2 != mp2.length)
s[i] = ( char )(it2
+ 'a' );
return String.valueOf(s);
}
public static void main(String[] args)
{
String str = "geeks" ;
System.out.println(findAltStr(str));
}
}
|
C#
using System;
class GFG {
static string FindAltStr( string str) {
char [] s = str.ToCharArray();
int n = s.Length;
int [] mp1 = new int [26];
int [] mp2 = new int [26];
int v = 0, c = 0;
foreach ( char ch in s) {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ) {
mp1[ch - 'a' ]++;
v++;
}
else {
mp2[ch - 'a' ]++;
c++;
}
}
if (Math.Abs(v - c) > 1)
return "no such string" ;
int it1 = 0, it2 = 0, i = 0;
while (it1 < mp1.Length && mp1[it1] == 0)
it1++;
while (it2 < mp2.Length && mp2[it2] == 0)
it2++;
bool f = c > v;
if (v == c) {
f = it1 > it2;
}
while ((it1 < mp1.Length && it2 < mp2.Length) && i < n) {
if (f) {
s[i] = ( char )(it2 + 'a' );
--mp2[it2];
while (it2 < mp2.Length && mp2[it2] == 0)
it2++;
f = false ;
}
else {
s[i] = ( char )(it1 + 'a' );
--mp1[it1];
while (it1 < mp1.Length && mp1[it1] == 0)
it1++;
f = true ;
}
++i;
}
if (it1 != mp1.Length)
s[i] = ( char )(it1 + 'a' );
else if (it2 != mp2.Length)
s[i] = ( char )(it2 + 'a' );
return new string (s);
}
public static void Main( string [] args) {
string str = "geeks" ;
Console.WriteLine(FindAltStr(str));
}
}
|
Python3
def findAltStr(s):
n = len (s)
mp1 = [ 0 ] * 26
mp2 = [ 0 ] * 26
v = 0
c = 0
for ch in s:
if ch in [ 'a' , 'e' , 'i' , 'o' , 'u' ]:
mp1[ ord (ch) - ord ( 'a' )] + = 1
v + = 1
else :
mp2[ ord (ch) - ord ( 'a' )] + = 1
c + = 1
if abs (v - c) > 1 :
return "no such string"
it1 = 0
it2 = 0
i = 0
while it1 < len (mp1) and mp1[it1] = = 0 :
it1 + = 1
while it2 < len (mp2) and mp2[it2] = = 0 :
it2 + = 1
f = c > v
if v = = c:
f = it1 > it2
new_str = [''] * n
while it1 < len (mp1) and it2 < len (mp2) and i < n:
if f:
new_str[i] = chr (it2 + ord ( 'a' ))
mp2[it2] - = 1
while it2 < len (mp2) and mp2[it2] = = 0 :
it2 + = 1
f = False
else :
new_str[i] = chr (it1 + ord ( 'a' ))
mp1[it1] - = 1
while it1 < len (mp1) and mp1[it1] = = 0 :
it1 + = 1
f = True
i + = 1
if it1 ! = len (mp1):
new_str[i] = chr (it1 + ord ( 'a' ))
elif it2 ! = len (mp2):
new_str[i] = chr (it2 + ord ( 'a' ))
return ''.join(new_str)
str = "geeks"
print (findAltStr( str ))
|
Javascript
function findAltStr(s) {
const n = s.length;
const mp1 = new Array(26).fill(0);
const mp2 = new Array(26).fill(0);
let v = 0;
let c = 0;
for (const ch of s) {
if ([ 'a' , 'e' , 'i' , 'o' , 'u' ].includes(ch)) {
mp1[ch.charCodeAt(0) - 'a '.charCodeAt(0)] += 1;
v += 1;
} else { // consonant
mp2[ch.charCodeAt(0) - ' a '.charCodeAt(0)] += 1;
c += 1;
}
}
if (Math.abs(v - c) > 1) {
return "no such string"; // if their diff is greater than one
// then string with alternate vowel and
// consonant cannot be made
}
let it1 = 0;
let it2 = 0;
let i = 0;
while (it1 < mp1.length && mp1[it1] == 0) {
it1 += 1; // to find first vowel
}
while (it2 < mp2.length && mp2[it2] == 0) {
it2 += 1; // to find first consonant
}
let f = c > v; // if number of consonant is greater then
// we will place consonant first else vowel
if (v === c) {
f = it1 > it2; // if both are equal then check which
// is lexiographically smaller
}
const new_str = new Array(n);
while (it1 < mp1.length && it2 < mp2.length && i < n) {
if (f) {
new_str[i] = String.fromCharCode(it2 + ' a '.charCodeAt(0));
mp2[it2] -= 1;
while (it2 < mp2.length && mp2[it2] === 0) {
it2 += 1;
}
f = false; // this will trigger to place vowel
// next
} else {
new_str[i] = String.fromCharCode(it1 + ' a '.charCodeAt(0));
mp1[it1] -= 1;
while (it1 < mp1.length && mp1[it1] === 0) {
it1 += 1;
}
f = true; // this will trigger to place
// consonant next
}
i += 1;
}
if (it1 !== mp1.length) {
new_str[i] = String.fromCharCode(it1 + ' a '.charCodeAt(0)); // if one vowel left
} else if (it2 !== mp2.length) {
new_str[i] = String.fromCharCode(it2 + ' a '.charCodeAt(0)); // if one consonant left
}
return new_str.join(' ');
}
const str = "geeks" ;
console.log(findAltStr(str));
|
Time Complexity: O(n)
Auxiliary Space: O(n)
This approach is contributed by Prateek Kumar Singh (pkrsingh025).
This 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.