Skip to content
Related Articles

Related Articles

Find the count of substrings in alphabetic order
  • Difficulty Level : Easy
  • Last Updated : 20 May, 2019

Given a string of length N 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 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.

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++




// CPP to find the number of substrings
// in alphabetical order
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find number of substrings
int 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 function
int main()
{
    string str = "alphabet";
  
    cout << findSubstringCount(str) << endl;
  
    return 0;
}

Java




// Java to find the number of substrings 
// in alphabetical order 
import java.util.*;
class Solution
{
    
// Function to find number of substrings 
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 (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 function 
public static void main(String args[])
    String str = "alphabet"
    
    System.out.println(findSubstringCount(str)); 
    
  
}
//contributed by Arnab Kundu

Python3




# Python3 to find the number of substrings
# in alphabetical order
  
# Function to find number of substrings
def 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 Code
if __name__ == "__main__":
  
    str = "alphabet"
  
    print(findSubstringCount(str))
  
# This code is contributed by ChitraNayal

C#




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
Output:
1

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :