Given a string str of lowercase alphabets, the task is to find the length of the longest sub-string of characters in alphabetical order i.e. string “dfabck” will return 3. Note that the alphabetical order here is considered circular i.e. a, b, c, d, e, …, x, y, z, a, b, c, ….
Input: str = “abcabcdefabc”
All valid sub-strings are “abc”, “abcdef” and “abc”
And, the length of the longest of these is 6
Input: str = “zabcd”
- Initialize i = 0 and len = 0 and starting from i find the ending index of the longest valid sub-string and store it in end.
- Now, update len = max(end – i + 1, len) and i = end + 1 (to get the next valid sub-string starting from the index end + 1) and repeat step 1 until i < len(str).
- Print the value of len in the end.
Below is the implementation of the above approach:
Time Complexity : O(n) where n is length of the input string. Note that we increase index by end.
- Longest substring of only 4's from the first N characters of the infinite string
- Minimum characters to be replaced to remove the given substring
- Suffix Tree Application 3 - Longest Repeated Substring
- Suffix Tree Application 6 - Longest Palindromic Substring
- Suffix Tree Application 5 - Longest Common Substring
- Group consecutive characters of same type in a string
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Substring Sort
- Frequency of a substring in a string
- Check if a string is substring of another
- Count number of Distinct Substring in a String
- Suffix Tree Application 1 - Substring Check
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.