Find the count of substrings in alphabetic order

30 Apr, 2021
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.Examples:  
Input : str = "refjhlmnbv"
Output : 2
Substrings are: "ef", "mn"

Input : str = "qwertyuiopasdfghjklzxcvbnm"
Output : 3
 
For a substring to be in alphabetical order its character is in the same sequence as they occur in English alphabets. Also, the ASCII value of consecutive characters in such substring differs by exactly 1. For finding a total number of substrings that 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 the next character in the string which is not in alphabetic order to its former character.Algorithm : 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:  
C++
Java
Python3
C#
Javascript
// CPP to find the number of substrings// in alphabetical order #include <bits/stdc++.h>using namespace std; // Function to find number of substringsint findSubstringCount(string str){    int result = 0;    int n = str.size();     // Iterate over string length    for (int i = 0; i < n - 1; i++) {        // if any two chars are in alphabetic order        if (str[i] + 1 == str[i + 1]) {            result++;            // find next char not in order            while (str[i] + 1 == str[i + 1]) {                i++;            }        }    }     // return the result    return result;} // Driver functionint main(){    string str = "alphabet";     cout << findSubstringCount(str) << endl;     return 0;}
// Java to find the number of substrings// in alphabetical orderimport java.util.*;class Solution{   // Function to find number of substringsstatic int findSubstringCount(String str){    int result = 0;    int n = str.length();       // Iterate over string length    for (int i = 0; i < n - 1; i++) {        // if any two chars are in alphabetic order        if (str.charAt(i) + 1 == str.charAt(i+1)) {            result++;            // find next char not in order            while (str.charAt(i) + 1 == str.charAt(i+1)) {                i++;            }        }    }       // return the result    return result;}   // Driver functionpublic static void main(String args[]){    String str = "alphabet";       System.out.println(findSubstringCount(str));   } }//contributed by Arnab Kundu
# Python3 to find the number of substrings# in alphabetical order # Function to find number of substringsdef findSubstringCount(str):     result = 0    n = len (str)     # Iterate over string length    for i in range (n - 1) :                 # if any two chars are in alphabetic order        if (ord(str[i]) + 1 == ord(str[i + 1])) :            result += 1                         # find next char not in order            while (ord(str[i]) + 1 == ord(str[i + 1])) :                i += 1     # return the result    return result # Driver Codeif __name__ == "__main__":     str = "alphabet"     print(findSubstringCount(str)) # This code is contributed by ChitraNayal
using System; // C# to find the number of substrings // in alphabetical order public class Solution{ // Function to find number of substrings public static int findSubstringCount(string str){    int result = 0;    int n = str.Length;     // Iterate over string length     for (int i = 0; i < n - 1; i++)    {        // if any two chars are in alphabetic order         if ((char)(str[i] + 1) == str[i + 1])        {            result++;            // find next char not in order             while ((char)(str[i] + 1) == str[i + 1])            {                i++;            }        }    }     // return the result     return result;} // Driver function public static void Main(string[] args){    string str = "alphabet";     Console.WriteLine(findSubstringCount(str)); } } // This code is contributed by Shrikant13
<script> // javascript to find the number of substrings// in alphabetical order // Function to find number of substringsfunction findSubstringCount(str){    var result = 0;    var n = str.length;     // Iterate over string length    for (var i = 0; i < n - 1; i++) {        // if any two chars are in alphabetic order        if (String.fromCharCode(str[i].charCodeAt(0) + 1) == str[i + 1]) {            result++;            // find next char not in order            while (String.fromCharCode(str[i].charCodeAt(0) + 1) === str[i + 1]) {                i++;            }        }    }     // return the result    return result;} // Driver functionvar str = "alphabet";document.write( findSubstringCount(str)); </script>
1
 
andrew1234
shrikanth13
ukasp
shubham_singh
itsok
substring
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.