Change language

Change the string so that it contains all vowels at least once

|

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, this is always impossible. 
For a line longer than 5, this is always possible. Just loop over each character and replace it with a vowel that isn’t on the string. If the current character is a vowel, and if it has not been visited before, then we will not change the character to a vowel. If all vowels are already present at the beginning, then there is 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 for storing different vowels

  vector " char " y; 

int length = str. length (); 

 

// if the line length is less than 5

  // then always impossible

if (length "5)

cout "& lt;  "Impossible" "& lt; endl; 

else  

  {

  / / Keeping different vowels in the string

// checking if the list contains a string and not

// in a list of different 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]); 

}

 

  // Save vowels

// missing in line

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 required

  if (z.empty ())

  cout "& lt; str "& lt; endl; 

else  

  {

  int cc = 0; 

vector " char " y; 

 

// Replace 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 "& lt; str "& lt; endl; 

}

}

}

 
// Driver code

int main ( int argc, char const * argv []) 

{

  string str = "ABCDEFGHI"

addAllVowel (str); 

return 0; 

}

 
// This code is provided
// sanjeev2552

python3

# Python implementation of the above approach

 

s = "ABCDEFGHI"

 
# convert string to list

S = list (s) 

  

  # All vowels

x = [ "A" , "E" , "I" , "O" , "U "

  

  # List for storing different vowels

y = [] 

le = len (S)

if (le & lt;  5 ):

  # if the line length is less than 5, then always

# Not possible

print ( "Impossible" )

else :

# Storing different vowels in a string

# checking if a string is in the list and not

# in the list of different vowels

  for i in range (le):

if (S [i] in x and S [i] not in y):

  y.append (S [i])

 

# Storing vowels that are not on the line

z = []

  for i in range ( 5 ):

  if (x [i] not in y):

z.append (x [i])

if ( len (z) = = 0 ):

  # No need to replace condition

print (s)

else :

  cc = 0

y = []

 

# Replace 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 = " ")

Exit:

 AOUDEFGHI 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically