Construct an Array of Strings having Longest Common Prefix specified by the given Array | C ++ Program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > solve ( int n , int arr [ ] ) { string s = string ( n , ' a ' ) ; vector < string > ans ; ans . push_back ( s ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { char ch = s [ arr [ i ] ] ; if ( ch == ' b ' ) ch = ' a ' ; else ch = ' b ' ; s [ arr [ i ] ] = ch ; ans . push_back ( s ) ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 0 , 3 } ; int n = sizeof arr / sizeof arr [ 0 ] ; vector < string > ans = solve ( n , arr ) ; for ( int i = ans . size ( ) - 1 ; i >= 0 ; i -- ) { cout << ans [ i ] << endl ; } return 0 ; }