Consider all remaining substrings . We consider every substring ending with index i .
for ( int i = k ; i < s . Length ; i ++ ) { currStr = currStr . Substring ( 1 , k - 1 ) + " " + s [ i ] ; if ( CompareTo ( lexMax , currStr ) < 0 ) lexMax = currStr ; if ( CompareTo ( lexMin , currStr ) > 0 ) lexMin = currStr ; }