Length of the longest substring with consecutive characters | Function to return the ending index for the largest valid sub - str1ing 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 - str1ing of consecutive characters from str1 ; Valid sub - str1ing exists from index i to end ; Update the Length ; Driver code
def getEndingIndex ( str1 , n , i ) : NEW_LINE INDENT i += 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT curr = str1 [ i ] NEW_LINE prev = str1 [ i - 1 ] NEW_LINE if ( ( curr == ' a ' and prev == ' z ' ) or ( ord ( curr ) - ord ( prev ) == 1 ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i - 1 NEW_LINE DEDENT def largestSubstr1 ( str1 , n ) : NEW_LINE INDENT Len = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT end = getEndingIndex ( str1 , n , i ) NEW_LINE Len = max ( end - i + 1 , Len ) NEW_LINE i = end + 1 NEW_LINE DEDENT return Len NEW_LINE DEDENT str1 = " abcabcdefabc " NEW_LINE n = len ( str1 ) NEW_LINE print ( largestSubstr1 ( str1 , n ) ) NEW_LINE