Modify the string such that it contains all vowels at least once | JavaScript 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
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 = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let y = [ ] ; let length = str . length ; if ( length < 5 ) document . write ( " " ) ; 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 + " " ) ; 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 = " " ; addAllVowel ( str ) ;