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
< ? php function getEndingIndex ( $ str , $ n , $ i ) { $ i ++ ; while ( $ i < $ n ) { $ curr = $ str [ $ i ] ; $ prev = $ str [ $ i - 1 ] ; if ( ( $ curr == ' a ' && $ prev == ' z ' ) || ( ord ( $ curr ) - ord ( $ prev ) == 1 ) ) $ i ++ ; else break ; } return $ i - 1 ; } function largestSubStr ( $ str , $ n ) { $ len = 0 ; $ i = 0 ; while ( $ i < $ n ) { $ end = getEndingIndex ( $ str , $ n , $ i ) ; $ len = max ( $ end - $ i + 1 , $ len ) ; $ i = $ end + 1 ; } return $ len ; } $ str = " abcabcdefabc " ; $ n = strlen ( $ str ) ; echo largestSubStr ( $ str , $ n ) ; ? >