Find the count of substrings in alphabetic order
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++
#include <bits/stdc++.h>
using namespace std;
int findSubstringCount(string str)
{
int result = 0;
int n = str.size();
for ( int i = 0; i < n - 1; i++) {
if (str[i] + 1 == str[i + 1]) {
result++;
while (str[i] + 1 == str[i + 1]) {
i++;
}
}
}
return result;
}
int main()
{
string str = "alphabet" ;
cout << findSubstringCount(str) << endl;
return 0;
}
|
Java
import java.util.*;
class Solution
{
static int findSubstringCount(String str)
{
int result = 0 ;
int n = str.length();
for ( int i = 0 ; i < n - 1 ; i++) {
if (str.charAt(i) + 1 == str.charAt(i+ 1 )) {
result++;
while (str.charAt(i) + 1 == str.charAt(i+ 1 )) {
i++;
}
}
}
return result;
}
public static void main(String args[])
{
String str = "alphabet" ;
System.out.println(findSubstringCount(str));
}
}
|
Python3
def findSubstringCount( str ):
result = 0
n = len ( str )
for i in range (n - 1 ) :
if ( ord ( str [i]) + 1 = = ord ( str [i + 1 ])) :
result + = 1
while ( ord ( str [i]) + 1 = = ord ( str [i + 1 ])) :
i + = 1
return result
if __name__ = = "__main__" :
str = "alphabet"
print (findSubstringCount( str ))
|
C#
using System;
public class Solution
{
public static int findSubstringCount( string str)
{
int result = 0;
int n = str.Length;
for ( int i = 0; i < n - 1; i++)
{
if (( char )(str[i] + 1) == str[i + 1])
{
result++;
while (( char )(str[i] + 1) == str[i + 1])
{
i++;
}
}
}
return result;
}
public static void Main( string [] args)
{
string str = "alphabet" ;
Console.WriteLine(findSubstringCount(str));
}
}
|
Javascript
<script>
function findSubstringCount(str)
{
var result = 0;
var n = str.length;
for ( var i = 0; i < n - 1; i++) {
if (String.fromCharCode(str[i].charCodeAt(0) + 1) == str[i + 1]) {
result++;
while (String.fromCharCode(str[i].charCodeAt(0) + 1) === str[i + 1]) {
i++;
}
}
}
return result;
}
var str = "alphabet" ;
document.write( findSubstringCount(str));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1)
Last Updated :
07 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...