Iterate from the end of the string to generate substrings
for ( int j = str . length ( ) ; j > i ; j -- ) { String sub_str = str . substring ( i , j ) ; if ( ! st . contains ( sub_str ) ) st . add ( sub_str ) ;