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
using System ; using System . Collections . Generic ; class GFG { static List < string > solve ( int n , int [ ] arr ) { string s = " aaa " ; List < string > ans = new List < string > ( ) ; ans . Add ( s ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . Length - 1 >= arr [ i ] ) { char ch = s [ arr [ i ] ] ; if ( ch == ' b ' ) ch = ' a ' ; else ch = ' b ' ; char [ ] chr = s . ToCharArray ( ) ; chr [ arr [ i ] ] = ch ; s = new string ( chr ) ; } ans . Add ( s ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 0 , 3 } ; int n = arr . Length ; List < string > ans = solve ( n , arr ) ; for ( int i = ans . Count - 1 ; i >= 0 ; i -- ) { Console . WriteLine ( ans [ i ] ) ; } } }