Replace every vowels with lexicographically next vowel in a String
Given string str of size N, which contains lowercase English alphabets. The task is to replace each vowel with the next immediate vowel lexicographically, i.e.,
‘a’ will be replaced by ‘e’,
‘e’ will be replaced by ‘i’,
‘i’ will be replaced by ‘o’,
‘o’ will be replaced by ‘u’,
‘u’ will be replaced by ‘a’.
Examples:
Input: str = “geeksforgeeks”
Output: giiksfurgiiks
Explanation:
e is replace by i
o is replace by u
So final string will be “giiksfurgiiks”.
Input: str = “gfg”
Output: gfg
Approach: We will create a Hashing of size 5 to store all vowels so that the replacement can be done easily for each vowel.
- Create a map and store all vowels.
- Iterate the string elements from left to right.
- It the string element is a vowel, then change it to the next vowels.
- Finally, print the final string.
Here is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
string print_next_vovel_string(string str)
{
map< char , int > m;
m[ 'a' ] = 0;
m[ 'e' ] = 1;
m[ 'i' ] = 2;
m[ 'o' ] = 3;
m[ 'u' ] = 4;
char arr[5] = { 'a' , 'e' , 'i' , 'o' , 'u' };
int N = str.length();
for ( int i = 0; i < N; i++) {
char c = str[i];
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' ) {
int index = m + 1;
int newindex = index % 5;
str[i] = arr[newindex];
}
}
return str;
}
int main()
{
string str = "geeksforgeeks" ;
cout << print_next_vovel_string(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String print_next_vovel_String( char []str)
{
HashMap<Character,
Integer> m = new HashMap<Character,
Integer>();
m.put( 'a' , 0 );
m.put( 'e' , 1 );
m.put( 'i' , 2 );
m.put( 'o' , 3 );
m.put( 'u' , 4 );
char arr[] = { 'a' , 'e' , 'i' , 'o' , 'u' };
int N = str.length;
for ( int i = 0 ; i < N; i++)
{
char c = str[i];
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' )
{
int index = m.get(c) + 1 ;
int newindex = index % 5 ;
str[i] = arr[newindex];
}
}
return String.valueOf(str);
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
System.out.print(print_next_vovel_String(
str.toCharArray()));
}
}
|
Python3
def print_next_vovel_string(st):
m = {}
m[ 'a' ] = 0
m[ 'e' ] = 1
m[ 'i' ] = 2
m[ 'o' ] = 3
m[ 'u' ] = 4
arr = [ 'a' , 'e' , 'i' , 'o' , 'u' ]
N = len (st)
for i in range (N):
c = st[i]
if (c = = 'a' or c = = 'e' or
c = = 'i' or c = = 'o' or
c = = 'u' ):
index = m[st[i]] + 1
newindex = index % 5
st = st.replace(st[i], arr[newindex], 1 )
return st
if __name__ = = "__main__" :
st = "geeksforgeeks"
print (print_next_vovel_string(st))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static String print_next_vovel_String( char []str)
{
Dictionary< char ,
int > m = new Dictionary< char ,
int >();
m.Add( 'a' , 0);
m.Add( 'e' , 1);
m.Add( 'i' , 2);
m.Add( 'o' , 3);
m.Add( 'u' , 4);
char []arr = { 'a' , 'e' , 'i' , 'o' , 'u' };
int N = str.Length;
for ( int i = 0; i < N; i++)
{
char c = str[i];
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' )
{
int index = m + 1;
int newindex = index % 5;
str[i] = arr[newindex];
}
}
return String.Join( "" , str);
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
Console.Write(print_next_vovel_String(
str.ToCharArray()));
}
}
|
Javascript
<script>
function print_next_vovel_string(st) {
var m = {};
m[ "a" ] = 0;
m[ "e" ] = 1;
m[ "i" ] = 2;
m[ "o" ] = 3;
m[ "u" ] = 4;
var arr = [ "a" , "e" , "i" , "o" , "u" ];
var N = st.length;
for (let i = 0; i < N; i++) {
var c = st[i];
if (c === "a" || c === "e" || c === "i" || c === "o" || c === "u" ) {
index = m[st[i]] + 1;
newindex = index % 5;
st = st.replace(st[i], arr[newindex]);
}
}
return st;
}
var st = "geeksforgeeks" ;
document.write(print_next_vovel_string(st));
</script>
|
Time Complexity: O (N)
Auxiliary Space: O (1)
Last Updated :
06 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...