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:
- Print all the palindromic permutations of given string in alphabetic order
- Count substrings with same first and last characters
- Count substrings that contain all vowels | SET 2
- Count of total anagram substrings
- Count Substrings with equal number of 0s, 1s and 2s
- Count of substrings which contains a given character K times
- Number of substrings with count of each character as k
- Count of substrings of a binary string containing K ones
- Recursive solution to count substrings with same first and last characters
- Count substrings with each character occurring at most k times
- Count distinct substrings that contain some characters at most k times
- Count number of substrings with exactly k distinct characters
- Count of Palindromic substrings in an Index range
- Count number of distinct substrings of a given length
- 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 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.