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.
- 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
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 3
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 4
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 2
- Substring Sort
- Frequency of a substring in a string
- Check if a string is substring of another
- Lexicographically smallest and largest substring of size k
- Count number of Distinct Substring in a String
- Suffix Tree Application 1 - Substring Check
- Java substring() method memory leak issue and fix