K | C # implementation of the above approach ; Set to store the unique substring ; String to create each substring ; Adding to set ; Converting set into a list ; Sorting the strings int the list into lexicographical order ; Printing kth substring ; Driver Code
using System ; using System . Collections . Generic ; using System . Collections ; class GFG { public static void kThLexString ( string st , int k , int n ) { HashSet < string > z = new HashSet < string > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string pp = " " ; for ( int j = i ; j < i + k ; j ++ ) { if ( j >= n ) break ; pp += st [ j ] ; z . Add ( pp ) ; } } ArrayList fin = new ArrayList ( ) ; foreach ( string s in z ) { fin . Add ( s ) ; } fin . Sort ( ) ; Console . Write ( fin [ k - 1 ] ) ; } public static void Main ( string [ ] args ) { string s = " geeksforgeeks " ; int k = 5 ; int n = s . Length ; kThLexString ( s , k , n ) ; } }