Given a string of length consisting of lowercase alphabets. The task is to find the number of such substrings whose characters occur in alphabetical order. Minimum allowed length of substring is 2.
Input : str = "refjhlmnbv" Output : 2 Substrings are: "ef", "mn" Input : str = "qwertyuiopasdfghjklzxcvbnm" Output : 3
For a substring to be in alphabetical order its character are in same sequence as they occur in english alphabets. Also, the ASCII value of consecutive characters in such substring differ by exactly 1. For finding total number of substrings which are in alphabetical order traverse the given string and compare two neighboring characters, if they are in alphabetic order increment the result and then find then next character in string which is not in alphabetic order to its former character.
Iterate over string length:
- if str[i]+1 == str[i+1] -> increase the result by 1 and iterate the string till next character which is out of alphabetic order
- else continue
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all the palindromic permutations of given string in alphabetic order
- Find the largest Alphabetic character present in the string
- Split numeric, alphabetic and special symbols from a String
- Count ways to split a Binary String into three substrings having equal count of zeros
- Queries to find the count of vowels in the substrings of the given string
- Given a binary string, count number of substrings that start and end with 1.
- Count number of substrings with exactly k distinct characters
- Count Substrings with equal number of 0s, 1s and 2s
- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- Count of total anagram substrings
- Count substrings with same first and last characters
- Count of Palindromic substrings in an Index range
- Recursive solution to count substrings with same first and last characters
- Number of substrings with count of each character as k
- Count of substrings of a binary string containing K ones
- Count substrings with each character occurring at most k times
- Permutation of given string that maximizes count of Palindromic substrings
- Count substrings that starts with character X and ends with character Y
- Count number of substrings with numeric value greater than X
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.