Construct an Array of Strings having Longest Common Prefix specified by the given Array | Function to find the array of strings ; Marks the ( N + 1 ) th string ; To generate remaining N strings ; Find i - th string using ( i + 1 ) - th string ; Check if current character is b ; Otherwise ; Insert the string ; Return the answer ; Driver Code ; Print the strings
def solve ( n , arr ) : NEW_LINE INDENT s = ' a ' * ( n ) NEW_LINE ans = [ ] NEW_LINE ans . append ( s ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if len ( s ) - 1 >= arr [ i ] : NEW_LINE ch = s [ arr [ i ] ] NEW_LINE if ( ch == ' b ' ) : NEW_LINE INDENT ch = ' a ' NEW_LINE DEDENT else : NEW_LINE INDENT ch = ' b ' NEW_LINE DEDENT p = list ( s ) NEW_LINE p [ arr [ i ] ] = ch NEW_LINE s = ' ' . join ( p ) NEW_LINE ans . append ( s ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 0 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE ans = solve ( n , arr ) NEW_LINE for i in range ( len ( ans ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( ans [ i ] ) NEW_LINE DEDENT DEDENT