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 . charAt ( i + j ) ; st . add ( s1 ) ; } } return st ; }