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 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 exists 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 early then no need to change any character.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; void addAllVowel(string str) { // All vowels char x[] = {'A', 'E', 'I', 'O', 'U'}; // List to store distinct vowels vector<char> y; int length = str.length(); // if length of string is less than 5 // then always Impossible if (length < 5) cout << "Impossible" << endl; else { // Storing the distinct vowels in the string // by checking if it in the list of string and not // in the list of distinct vowels 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]); } // Storing the vowels which are // not present in the string 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]); // No replacement needed condition if (z.empty()) cout << str << endl; else { int cc = 0; vector<char> y; // Replacing the characters to get all Vowels 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; } } } // Driver Code int main(int argc, char const *argv[]) { string str = "ABCDEFGHI"; addAllVowel(str); return 0; } // This code is contributed by // sanjeev2552 |
Java
// Java implementation of the approach 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) { // All vowels char x[] = {'A', 'E', 'I', 'O', 'U'}; // List to store distinct vowels Vector<Character> y = new Vector<>(); int length = str.length(); // if length of String is less than 5 // then always Impossible if (length < 5) System.out.println("Impossible"); else { // Storing the distinct vowels in the String // by checking if it in the list of String and not // in the list of distinct vowels for (int i = 0; i < length; i++) { if (find(x, str.charAt(i))&& !find(y, str.charAt(i))) y.add(str.charAt(i)); } // Storing the vowels which are // not present in the String Vector<Character> z = new Vector<>(); for (int i = 0; i < 5; i++) if (!find(y, x[i]) ) z.add(x[i]); // No replacement needed condition if (z.size() == 0) System.out.println( str ); else { int cc = 0; Vector<Character> y1 = new Vector<>(); String ans = ""; // Replacing the characters to get all Vowels 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()) { //copy th rest of the string for(int j = i + 1; j < length; j++) ans += str.charAt(j); break; } } System.out.println(ans); } } } // Driver Code public static void main(String args[]) { String str = "ABCDEFGHI"; addAllVowel(str); } } // This code is contributed by Arnab Kundu |
Python3
# Python implementation of the above approach s = "ABCDEFGHI" # converting String to a list S = list(s) # All vowels x = ["A", "E", "I", "O", "U"] # List to store distinct vowels y = [] le = len(S) if (le < 5): # if length of string is less than 5 then always # Impossible print("Impossible") else: # Storing the distinct vowels in the string # by checking if it in the list of string and not # in the list of distinct vowels for i in range(le): if (S[i] in x and S[i] not in y): y.append(S[i]) # Storing the vowels which are not present in the string z = [] for i in range(5): if (x[i] not in y): z.append(x[i]) if (len(z) == 0): # No replacement needed condition print(s) else: cc = 0 y = [] # Replacing the characters to get all Vowels 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 ="") |
AOUDEFGHI
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Modify string by removing vowels in between two consonants
- Modify the string by swapping continuous vowels or consonants
- Check if a string can be converted to another string by replacing vowels and consonants
- Minimum cost to modify a string
- Modify Numpy array to store an arbitrary length string
- First X vowels from a string
- Reverse vowels in a given string
- Program to remove vowels from a String
- Longest Subsequence of a String containing only vowels
- Permutations of string such that no two vowels are adjacent
- Remove consecutive vowels from string
- Count the pairs of vowels in the given string
- Program to duplicate Vowels in String
- Python | Count and display vowels in a string
- Replace every vowels with lexicographically next vowel in a String
- Python | Alternate vowels and consonents in String
- Check whether the vowels in a string are in alphabetical order or not
- Replace all consonants with nearest vowels in a string
- Print reverse string after removing vowels
- Program to count vowels in a string (Iterative and Recursive)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

