Construct an Array of Strings having Longest Common Prefix specified by the given Array | Java Program to implement the above approach ; Function to find the array of strings ; Marks the ( N + 1 ) th string ; To generate remaining N strings ; Check if current character is b ; Find i - th string using ( i + 1 ) - th string ; Otherwise ; Insert the string ; Return the answer ; Driver Code ; Print the strings
import java . util . * ; class GFG { static Vector < String > solve ( int n , int arr [ ] ) { String s = " aaa " ; Vector < String > ans = new Vector < String > ( ) ; ans . add ( s ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . length ( ) - 1 >= arr [ i ] ) { char ch = s . charAt ( arr [ i ] ) ; if ( ch == ' b ' ) ch = ' a ' ; else ch = ' b ' ; char [ ] myNameChars = s . toCharArray ( ) ; myNameChars [ arr [ i ] ] = ch ; s = String . valueOf ( myNameChars ) ; } ans . add ( s ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 0 , 3 } ; int n = arr . length ; Vector < String > ans = solve ( n , arr ) ; for ( int i = ans . size ( ) - 1 ; i >= 0 ; i -- ) { System . out . println ( ans . get ( i ) ) ; } } }