Queries to answer the X | C # code for above given approach ; Function to pre - process the sub - strings in sorted order ; Generate all substrings ; Iterate to find all sub - strings ; Store the sub - string in the vector ; Sort the substrings lexicographically ; sort ( substrings . begin ( ) , substrings . end ( ) ) ; ; Driver code ; To store all the sub - strings ; Perform queries
using System ; class GFG { static void pre_process ( String [ ] substrings , String s ) { int n = s . Length ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { String dup = " " ; for ( int j = i ; j < n ; j ++ ) { dup += s [ j ] ; substrings [ count ++ ] = dup ; } } int size = substrings . Length ; for ( int i = 0 ; i < size - 1 ; i ++ ) { for ( int j = i + 1 ; j < substrings . Length ; j ++ ) { if ( substrings [ i ] . CompareTo ( substrings [ j ] ) > 0 ) { String temp = substrings [ i ] ; substrings [ i ] = substrings [ j ] ; substrings [ j ] = temp ; } } } } public static void Main ( String [ ] args ) { String s = " geek " ; String [ ] substrings = new String [ 10 ] ; pre_process ( substrings , s ) ; int [ ] queries = { 1 , 5 , 10 } ; int q = queries . Length ; for ( int i = 0 ; i < q ; i ++ ) Console . WriteLine ( substrings [ queries [ i ] - 1 ] ) ; } }