Length of the longest substring with consecutive characters | Function to return the ending index for the largest valid sub - string starting from index i ; If the current character appears after the previous character according to the given circular alphabetical order ; Function to return the length of the longest sub - string of consecutive characters from str ; Valid sub - string exists from index i to end ; Update the length ; Driver code
function getEndingIndex ( str , n , i ) { i ++ ; while ( i < n ) { let curr = str [ i ] ; let prev = str [ i - 1 ] ; if ( ( curr == ' ' && prev == ' ' ) || ( curr . charCodeAt ( 0 ) - prev . charCodeAt ( 0 ) == 1 ) ) i ++ ; else break ; } return i - 1 ; } function largestSubStr ( str , n ) { let len = 0 ; let i = 0 ; while ( i < n ) { let end = getEndingIndex ( str , n , i ) ; len = Math . max ( end - i + 1 , len ) ; i = end + 1 ; } return len ; } let str = " " ; let n = str . length ; document . write ( largestSubStr ( str , n ) ) ;