Count of increasing substrings in given String
Last Updated :
17 Nov, 2021
Given string str of length N, the task is to print the number of substrings in which every character’s ASCII value is greater or equal to the ASCII value of the previous character. The substrings must at least be of length 2.
Example:
Input: str = “bcdabc”
Output: 6
Explanation: The 6 substrings with alphabets having greater ASCII value than the previous character and length at least 2 are bc, bcd, cd, ab, abc, bc
Input: str = “cegxza”
Output: 10
Naive Approach: The above problem can be iterating the string and counting increasing ASCII value substrings starting at every index.
Time Complexity: O(N^2)
Approach: An efficient approach for the given problem would be to iterate the string and use a mathematical calculation to find all possible increasing ASCII value substrings in a larger substring. Follow the approach below to solve the problem:
- Initialize pointers left and right to 0
- Initialize a variable ans to store the answer
- Iterate the string until the right pointer is less than the length of the string:
- Use a while loop to iterate using the right pointer until str[right] >= str[right – 1] and right < str.length()
- Calculate the number of substrings having increasing ASCII value by adding initializing len = right – left, then adding then value of (len * (len + 1) / 2) – len to ans
- Make left = right and the right pointer will get incremented for the next iteration
- Return ans as our result
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countIncSub(string str)
{
int left = 0, right = 1;
int l = str.length();
int ans = 0;
for (; right < l; right++)
{
while (right < l && str[right] >= str[right - 1])
{
right++;
}
int len = right - left;
ans += (len * (len + 1)) / 2 - len;
left = right;
}
return ans;
}
int main()
{
string str = "cegxza" ;
cout << (countIncSub(str));
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int countIncSub(String str)
{
int left = 0 , right = 1 ;
int l = str.length();
int ans = 0 ;
for (; right < l; right++) {
while (right < l
&& str.charAt(right)
>= str.charAt(right - 1 )) {
right++;
}
int len = right - left;
ans += (len * (len + 1 )) / 2 - len;
left = right;
}
return ans;
}
public static void main(String[] args)
{
String str = "cegxza" ;
System.out.println(countIncSub(str));
}
}
|
Python3
def countIncSub( str ):
left = 0
right = 1
l = len ( str )
ans = 0
for right in range ( 1 , l):
while (right < l and str [right] > = str [right - 1 ]):
right + = 1
length = right - left
ans + = (length * (length + 1 )) / / 2 - length
left = right
return ans
str = "cegxza"
print (countIncSub( str ))
|
C#
using System;
using System.Collections;
class GFG {
static int countIncSub( string str)
{
int left = 0, right = 1;
int l = str.Length;
int ans = 0;
for (; right < l; right++) {
while (right < l
&& str[right]
>= str[right - 1]) {
right++;
}
int len = right - left;
ans += (len * (len + 1)) / 2 - len;
left = right;
}
return ans;
}
public static void Main()
{
string str = "cegxza" ;
Console.Write(countIncSub(str));
}
}
|
Javascript
<script>
function countIncSub(str)
{
let left = 0, right = 1;
let l = str.length;
let ans = 0;
for (; right < l; right++)
{
while (right < l && str[right] >= str[right - 1])
{
right++;
}
let len = right - left;
ans += (len * (len + 1)) / 2 - len;
left = right;
}
return ans;
}
let str = "cegxza" ;
document.write(countIncSub(str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...