Function to return a set of all substrings of given string which have length less than or equal to k
static HashSet < string > presentSubstring ( string s , int k ) { HashSet < string > st = new HashSet < string > ( ) ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { string s1 = " " ; for ( int j = 0 ; j < k && i + j < n ; j ++ ) { s1 += s [ i + j ] ; st . Add ( s1 ) ; } } return st ; }