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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find length of longest substring with at most K normal characters
- Longest substring with atmost K characters from the given set of characters
- Longest substring of only 4's from the first N characters of the infinite string
- Strings formed from given characters without any consecutive repeating characters
- Minimum characters to be replaced to remove the given substring
- Longest palindromic string possible after removal of a substring
- Suffix Tree Application 5 - Longest Common Substring
- Suffix Tree Application 3 - Longest Repeated Substring
- Suffix Tree Application 6 - Longest Palindromic Substring
- Check three or more consecutive identical characters or numbers
- Group consecutive characters of same type in a string
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Binary string with given frequencies of sums of consecutive pairs of characters
- Total length of string from given Array of strings composed using given characters
- Minimize number of cuts required to break N length stick into N unit length sticks
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Substring Sort
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.