Longest substring with no pair of adjacent characters are adjacent English alphabets | Function to find the longest substring satisfying the given condition ; Stores all temporary substrings ; Stores the longest substring ; Stores the length of the substring T ; Stores the first character of string S ; Traverse the string ; If the absolute difference is 1 ; Update the length of substring T ; Update the longest substring ; Otherwise , stores the current character ; Again checking for longest substring and update accordingly ; Print the longest substring ; Given string ; Function call to find the longest substring satisfying given condition
function findSubstring ( S ) { var T = " " ; var ans = " " ; var l = 0 ; T += S [ 0 ] ; for ( var i = 1 ; i < S . length ; i ++ ) { if ( Math . abs ( S [ i ] . charCodeAt ( 0 ) - S [ i - 1 ] . charCodeAt ( 0 ) ) === 1 ) { l = T . length ; if ( l > ans . length ) { ans = T ; } T = " " ; T += S [ i ] ; } else { T += S [ i ] ; } } l = T . length ; if ( l > ans . length ) { ans = T ; } document . write ( ans ) ; } var S = " " ; findSubstring ( S ) ;