Modify the string such that it contains all vowels at least once | C ++ 14 implementation of the above approach ; All vowels ; List to store distinct vowels ; 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 ; Storing the vowels which are not present in the string ; No replacement needed condition ; Replacing the characters to get all Vowels ; copy th rest of the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }