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++
// 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 |
Javascript
<script> // javascript to find the number of substrings // in alphabetical order // Function to find number of substrings function 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 function var str = "alphabet" ; document.write( findSubstringCount(str)); </script> |
1
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!