Remove the first and last character and call the function for all characters
for ( var c1 = ' ' . charCodeAt ( 0 ) ; c1 <= ' ' . charCodeAt ( 0 ) ; c1 ++ ) if ( String . fromCharCode ( c1 ) != c ) ans = Math . max ( ans , 1 + solve ( s . substring ( 1 , s . length - 1 ) , String . fromCharCode ( c1 ) ) ) ; }