Length of the longest substring with consecutive characters | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getEndingIndex ( string str , int n , int i ) { i ++ ; while ( i < n ) { char curr = str [ i ] ; char prev = str [ i - 1 ] ; if ( ( curr == ' a ' && prev == ' z ' ) || ( curr - prev == 1 ) ) i ++ ; else break ; } return i - 1 ; } int largestSubStr ( string str , int n ) { int len = 0 ; int i = 0 ; while ( i < n ) { int end = getEndingIndex ( str , n , i ) ; len = max ( end - i + 1 , len ) ; i = end + 1 ; } return len ; } int main ( ) { string str = " abcabcdefabc " ; int n = str . length ( ) ; cout << ( largestSubStr ( str , n ) ) ; }