Modify the string such that it contains all vowels at least once

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.


Input: str = "ABCDEFGHI"
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"

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:





# Python implementation of the above approach
# 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
    # 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):
    # Storing the vowels which are not present in the string
    z = []
    for i in range(5):
        if (x[i] not in y):
    if (len(z) == 0):
        # No replacement needed condition
        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):
                S[i] = z[cc]
                cc += 1
            if (cc == len(z)):
        print(*S, sep ="")




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.