Modify the string such that it contains all vowels at least once
Last Updated :
07 Mar, 2023
Given a string S containing only Uppercase letters, the task is to find the minimum number of replacement of characters needed to get a string with all vowels and if we cannot make the required string then print Impossible.
Examples:
Input: str = “ABCDEFGHI”
Output: AOUDEFGHI
Explanation: There are already 3 Vowels present in the string A, E, I we just change B and C to O and U respectively.
Input: str = “ABC”
Output: IMPOSSIBLE
Approach: Since there are only 5 vowels A, E, I, O, U. So, If the string length is less than 5 it is always impossible.
For a string of length greater than equal to 5, it is always possible. Just iterate over each character and replace it with the vowel that doesn’t exist in the string. If the current character is a vowel and if it is not visited earlier then we will not change the character to the vowel. If all the vowels are already present from an early then no need to change any character.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void addAllVowel(string str)
{
char x[] = { 'A' , 'E' , 'I' , 'O' , 'U' };
vector< char > y;
int length = str.length();
if (length < 5)
cout << "Impossible" << endl;
else
{
for ( int i = 0; i < length; i++)
{
if (find(x, x + 5, str[i]) != x + 5 and
find(y.begin(), y.end(), str[i]) == y.end())
y.push_back(str[i]);
}
vector< char > z;
for ( int i = 0; i < 5; i++)
if (find(y.begin(),
y.end(), x[i]) == y.end())
z.push_back(x[i]);
if (z.empty())
cout << str << endl;
else
{
int cc = 0;
vector< char > y;
for ( int i = 0; i < length; i++)
{
if (find(x, x + 5, str[i]) != x + 5 and
find(y.begin(), y.end(),
str[i]) == y.end())
y.push_back(str[i]);
else
{
str[i] = z[cc];
cc++;
}
if (cc == z.size()) break ;
}
cout << str << endl;
}
}
}
int main( int argc, char const *argv[])
{
string str = "ABCDEFGHI" ;
addAllVowel(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean find( char x[], char c)
{
for ( int i = 0 ; i < x.length; i++)
if (x[i] == c)
return true ;
return false ;
}
static boolean find(Vector<Character> v, char c)
{
for ( int i = 0 ; i < v.size(); i++)
if (v.get(i) == c)
return true ;
return false ;
}
static void addAllVowel(String str)
{
char x[] = { 'A' , 'E' , 'I' , 'O' , 'U' };
Vector<Character> y = new Vector<>();
int length = str.length();
if (length < 5 )
System.out.println( "Impossible" );
else
{
for ( int i = 0 ; i < length; i++)
{
if (find(x, str.charAt(i))&&
!find(y, str.charAt(i)))
y.add(str.charAt(i));
}
Vector<Character> z = new Vector<>();
for ( int i = 0 ; i < 5 ; i++)
if (!find(y, x[i]) )
z.add(x[i]);
if (z.size() == 0 )
System.out.println( str );
else
{
int cc = 0 ;
Vector<Character> y1 = new Vector<>();
String ans = "" ;
for ( int i = 0 ; i < length; i++)
{
if (find(x, str.charAt(i))&&
!find(y1, str.charAt(i)))
{
ans += str.charAt(i);
y1.add(str.charAt(i));
}
else
{
ans += z.get(cc);
cc++;
}
if (cc == z.size())
{
for ( int j = i + 1 ; j < length; j++)
ans += str.charAt(j);
break ;
}
}
System.out.println(ans);
}
}
}
public static void main(String args[])
{
String str = "ABCDEFGHI" ;
addAllVowel(str);
}
}
|
Python3
s = "ABCDEFGHI"
S = list (s)
x = [ "A" , "E" , "I" , "O" , "U" ]
y = []
le = len (S)
if (le < 5 ):
print ( "Impossible" )
else :
for i in range (le):
if (S[i] in x and S[i] not in y):
y.append(S[i])
z = []
for i in range ( 5 ):
if (x[i] not in y):
z.append(x[i])
if ( len (z) = = 0 ):
print (s)
else :
cc = 0
y = []
for i in range (le):
if (S[i] in x and S[i] not in y):
y.append(S[i])
else :
S[i] = z[cc]
cc + = 1
if (cc = = len (z)):
break
print ( * S, sep = "")
|
C#
using System;
using System.Collections;
class GFG{
static bool find( char []x, char c)
{
for ( int i = 0; i < x.Length; i++)
if (x[i] == c)
return true ;
return false ;
}
static bool find(ArrayList v, char c)
{
for ( int i = 0; i < v.Count; i++)
if (( char )v[i] == c)
return true ;
return false ;
}
static void addAllVowel( string str)
{
char []x = { 'A' , 'E' , 'I' , 'O' , 'U' };
ArrayList y = new ArrayList();
int length = str.Length;
if (length < 5)
Console.Write( "Impossible" );
else
{
for ( int i = 0; i < length; i++)
{
if (find(x, str[i]) &&
!find(y, str[i]))
y.Add(str[i]);
}
ArrayList z = new ArrayList();
for ( int i = 0; i < 5; i++)
if (!find(y, x[i]) )
z.Add(x[i]);
if (z.Count == 0)
Console.Write(str);
else
{
int cc = 0;
ArrayList y1 = new ArrayList();
string ans = "" ;
for ( int i = 0; i < length; i++)
{
if (find(x, str[i]) &&
!find(y1, str[i]))
{
ans += str[i];
y1.Add(str[i]);
}
else
{
ans += ( char )z[cc];
cc++;
}
if (cc == z.Count)
{
for ( int j = i + 1;
j < length; j++)
ans += str[j];
break ;
}
}
Console.Write(ans);
}
}
}
public static void Main( string []args)
{
string str = "ABCDEFGHI" ;
addAllVowel(str);
}
}
|
Javascript
<script>
function find(x,c)
{
for (let i = 0; i < x.length; i++)
if (x[i] == c)
return true ;
return false ;
}
function addAllVowel(str)
{
let x = [ 'A' , 'E' , 'I' , 'O' , 'U' ];
let y = [];
let length = str.length;
if (length < 5)
document.write( "Impossible<br>" );
else
{
for (let i = 0; i < length; i++)
{
if (find(x, str[i])&&
!find(y, str[i]))
y.push(str[i]);
}
let z = [];
for (let i = 0; i < 5; i++)
if (!find(y, x[i]) )
z.push(x[i]);
if (z.length == 0)
document.write( str+ "<br>" );
else
{
let cc = 0;
let y1 = [];
let ans = "" ;
for (let i = 0; i < length; i++)
{
if (find(x, str[i])&&
!find(y1, str[i]))
{
ans += str[i];
y1.push(str[i]);
}
else
{
ans += z[cc];
cc++;
}
if (cc == z.length)
{
for (let j = i + 1; j < length; j++)
ans += str[j];
break ;
}
}
document.write(ans);
}
}
}
let str = "ABCDEFGHI" ;
addAllVowel(str);
</script>
|
Complexity Analysis:
- Time Complexity: O(N), where N is the size of the string
- Auxiliary Space: O(1) because a constant size vector is being used to store vowels
Another approach is to use a dictionary to store the count of each vowel in the string. This can be done by iterating through the string and incrementing the count of each vowel in the dictionary. Then, we can iterate through the dictionary and check if any of the vowel counts are zero. If a vowel count is zero, we can replace a non-vowel character in the string with that vowel. This can be done by iterating through the string again and replacing the first non-vowel character that is encountered with the missing vowel.
Here is an example of this approach in Python:
C++
#include <iostream>
#include <map>
using namespace std;
string modifyString(string s)
{
map< char , int > vowels;
vowels[ 'A' ] = 0;
vowels[ 'E' ] = 0;
vowels[ 'I' ] = 0;
vowels[ 'O' ] = 0;
vowels[ 'U' ] = 0;
for ( int i = 0; i < s.length(); i++) {
char c = s[i];
if (vowels.count(c)) {
vowels++;
}
}
for ( int i = 0; i < s.length(); i++) {
char c = s[i];
if (!vowels.count(c)) {
for ( auto & kv : vowels) {
if (kv.second == 0) {
s[i] = kv.first;
kv.second = 1;
break ;
}
}
}
}
return s;
}
int main()
{
string s = "ABCDEFGHI" ;
string modified_s = modifyString(s);
cout << modified_s << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static String modifyString(String s) {
Map<Character, Integer> vowels = new HashMap<Character, Integer>();
vowels.put( 'A' , 0 );
vowels.put( 'E' , 0 );
vowels.put( 'I' , 0 );
vowels.put( 'O' , 0 );
vowels.put( 'U' , 0 );
for ( int i = 0 ; i < s.length(); i++) {
char c = s.charAt(i);
if (vowels.containsKey(c)) {
vowels.put(c, vowels.get(c) + 1 );
}
}
for ( int i = 0 ; i < s.length(); i++) {
char c = s.charAt(i);
if (!vowels.containsKey(c)) {
for ( char v : vowels.keySet()) {
if (vowels.get(v) == 0 ) {
s = s.substring( 0 , i) + v + s.substring(i+ 1 );
vowels.put(v, 1 );
break ;
}
}
}
}
return s;
}
public static void main(String[] args) {
String s = "ABCDEFGHI" ;
String modified_s = modifyString(s);
System.out.println(modified_s);
}
}
|
Python3
def modify_string(s):
vowels = { 'A' : 0 , 'E' : 0 , 'I' : 0 , 'O' : 0 , 'U' : 0 }
for c in s:
if c in vowels:
vowels + = 1
for c in s:
if c in vowels:
continue
for v in vowels:
if vowels[v] = = 0 :
s = s.replace(c, v, 1 )
vowels[v] = 1
break
return s
s = "ABCDEFGHI"
modified_s = modify_string(s)
print (modified_s)
|
Javascript
function modifyString(s)
{
let vowels = { 'A' : 0, 'E' : 0, 'I' : 0, 'O' : 0, 'U' : 0};
for (let i = 0; i < s.length; i++) {
let c = s[i];
if (c in vowels) {
vowels += 1;
}
}
for (let i = 0; i < s.length; i++) {
let c = s[i];
if (c in vowels) {
continue ;
}
for (let v in vowels) {
if (vowels[v] == 0) {
s = s.replace(c, v);
vowels[v] = 1;
break ;
}
}
}
return s;
}
let s = "ABCDEFGHI" ;
let modified_s = modifyString(s);
console.log(modified_s);
|
C#
using System;
using System.Collections.Generic;
class Program
{
static string ModifyString( string s)
{
Dictionary< char , int > vowels = new Dictionary< char , int >();
vowels[ 'A' ] = 0;
vowels[ 'E' ] = 0;
vowels[ 'I' ] = 0;
vowels[ 'O' ] = 0;
vowels[ 'U' ] = 0;
for ( int i = 0; i < s.Length; i++)
{
char c = s[i];
if (vowels.ContainsKey(c))
{
vowels++;
}
}
for ( int i = 0; i < s.Length; i++)
{
char c = s[i];
if (!vowels.ContainsKey(c))
{
foreach ( var kv in vowels)
{
if (kv.Value == 0)
{
s = s.Remove(i, 1).Insert(i, kv.Key.ToString());
vowels[kv.Key] = 1;
break ;
}
}
}
}
return s;
}
static void Main()
{
string s = "ABCDEFGHI" ;
string modified_s = ModifyString(s);
Console.WriteLine(modified_s);
}
}
|
Time complexity: O(N) as we iterate through the string twice.
Auxiliary space: O(1) as we only use a fixed-size dictionary to store the vowel counts.
Share your thoughts in the comments
Please Login to comment...