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++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count number of substrings
// with increasing ascii values
int countIncSub(string str)
{
// Initialize left and right pointers
int left = 0, right = 1;
// Initialize length of the string str
int l = str.length();
// Initialize a variable to store answer
int ans = 0;
// Iterate the string
for (; right < l; right++)
{
// Iterate the while loop until the
// string has increasing ASCII value
while (right < l && str[right] >= str[right - 1])
{
right++;
}
// Calculate length of longest
// substring found starting from left
int len = right - left;
// Calculate the number of substrings
// with length at least 2 and add it
// to the ans
ans += (len * (len + 1)) / 2 - len;
// Update left equal to right
left = right;
}
// Return the answer
return ans;
}
int main()
{
// Initialize the string str
string str = "cegxza";
// Call the function and
// print the result
cout << (countIncSub(str));
}
// This code is contributed by Potta Lokesh
Java
// Java implementation for the above approach
import java.io.*;
import java.util.*;
// Driver code
class GFG {
// Function to count number of substrings
// with increasing ascii values
public static int countIncSub(String str)
{
// Initialize left and right pointers
int left = 0, right = 1;
// Initialize length of the string str
int l = str.length();
// Initialize a variable to store answer
int ans = 0;
// Iterate the string
for (; right < l; right++) {
// Iterate the while loop until the
// string has increasing ASCII value
while (right < l
&& str.charAt(right)
>= str.charAt(right - 1)) {
right++;
}
// Calculate length of longest
// substring found starting from left
int len = right - left;
// Calculate the number of substrings
// with length at least 2 and add it
// to the ans
ans += (len * (len + 1)) / 2 - len;
// Update left equal to right
left = right;
}
// Return the answer
return ans;
}
public static void main(String[] args)
{
// Initialize the string str
String str = "cegxza";
// Call the function and
// print the result
System.out.println(countIncSub(str));
}
}
Python3
# Python code for the above approach
# Function to count number of substrings
# with increasing ascii values
def countIncSub(str):
# Initialize left and right pointers
left = 0
right = 1
# Initialize length of the string str
l = len(str)
# Initialize a variable to store answer
ans = 0
# Iterate the string
for right in range(1, l):
# Iterate the while loop until the
# string has increasing ASCII value
while (right < l and str[right] >= str[right - 1]):
right += 1
# Calculate length of longest
# substring found starting from left
length = right - left
# Calculate the number of substrings
# with length at least 2 and add it
# to the ans
ans += (length * (length + 1)) // 2 - length
# Update left equal to right
left = right
# Return the answer
return ans
# Driver Code
# Initialize the string str
str = "cegxza"
# Call the function and
# print the result
print(countIncSub(str))
# This code is contributed by Saurabh Jaiswal
C#
// C# implementation for the above approach
using System;
using System.Collections;
// Driver code
class GFG {
// Function to count number of substrings
// with increasing ascii values
static int countIncSub(string str)
{
// Initialize left and right pointers
int left = 0, right = 1;
// Initialize length of the string str
int l = str.Length;
// Initialize a variable to store answer
int ans = 0;
// Iterate the string
for (; right < l; right++) {
// Iterate the while loop until the
// string has increasing ASCII value
while (right < l
&& str[right]
>= str[right - 1]) {
right++;
}
// Calculate length of longest
// substring found starting from left
int len = right - left;
// Calculate the number of substrings
// with length at least 2 and add it
// to the ans
ans += (len * (len + 1)) / 2 - len;
// Update left equal to right
left = right;
}
// Return the answer
return ans;
}
public static void Main()
{
// Initialize the string str
string str = "cegxza";
// Call the function and
// print the result
Console.Write(countIncSub(str));
}
}
// This code is contributed by Samim Hossain Mondal
JavaScript
<script>
// Javascript code for the above approach
// Function to count number of substrings
// with increasing ascii values
function countIncSub(str)
{
// Initialize left and right pointers
let left = 0, right = 1;
// Initialize length of the string str
let l = str.length;
// Initialize a variable to store answer
let ans = 0;
// Iterate the string
for (; right < l; right++)
{
// Iterate the while loop until the
// string has increasing ASCII value
while (right < l && str[right] >= str[right - 1])
{
right++;
}
// Calculate length of longest
// substring found starting from left
let len = right - left;
// Calculate the number of substrings
// with length at least 2 and add it
// to the ans
ans += (len * (len + 1)) / 2 - len;
// Update left equal to right
left = right;
}
// Return the answer
return ans;
}
// Driver Code
// Initialize the string str
let str = "cegxza";
// Call the function and
// print the result
document.write(countIncSub(str));
// This code is contributed by Samim Hossain Mondal
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count of Reverse Bitonic Substrings in a given String Given a string S, the task is to count the number of Reverse Bitonic Substrings in the given string. Reverse bitonic substring: A string in which the ASCII values of the characters of the string follow any of the following patterns: Strictly IncreasingStrictly decreasingDecreasing and then increasin
8 min read
Count subsequence of length three in a given string Given a string of length n and a subsequence of length 3. Find the total number of occurrences of the subsequence in this string. Examples : Input : string = "GFGFGYSYIOIWIN", subsequence = "GFG" Output : 4 Explanation : There are 4 such subsequences as shown: GFGFGYSYIOIWIN GFGFGYSYIOIWIN GFGFGYSYI
15 min read
Count of Distinct Substrings occurring consecutively in a given String Given a string str, the task is to find the number of distinct substrings that are placed consecutively in the given string. Examples: Input: str = "geeksgeeksforgeeks" Output: 2 Explanation: geeksgeeksforgeeks -> {"geeks"} geeksgeeksforgeeks -> {"e"} Only one consecutive occurrence of "e" is
14 min read
Count of 'GFG' Subsequences in the given string Given a string of length n of capital letters. The task is to find the count of 'GFG' subsequence in the given string. Examples: Input : str[] = "GFGFG" Output : 4 GFGFG, GFGFG, GFGFG, GFGFG Input : str[] = "ABCFGFPG" Output : 1 To find the number of "GFG" subsequences in the given string, observe f
5 min read
Count of bitonic substrings from the given string Given a string str, the task is to count all the bitonic substrings of the given string. A bitonic substring is a substring of the given string in which elements are either strictly increasing or strictly decreasing, or first increasing and then decreasing. Examples: Input: str = "bade"Output: 8Expl
7 min read
Find distinct characters in distinct substrings of a string Given a string str, the task is to find the count of distinct characters in all the distinct sub-strings of the given string.Examples: Input: str = "ABCA" Output: 18 Distinct sub-stringsDistinct charactersA1AB2ABC3ABCA3B1BC2BCA3C1CA2 Hence, 1 + 2 + 3 + 3 + 1 + 2 + 3 + 1 + 2 = 18Input: str = "AAAB" O
5 min read
Count M-length substrings occurring exactly K times in a string Given a string S of length N and two integers M and K, the task is to count the number of substrings of length M occurring exactly K times in the string S. Examples: Input: S = "abacaba", M = 3, K = 2Output: 1Explanation: All distinct substrings of length 3 are "aba", "bac", "aca", "cab".Out of all
15+ min read
Count all palindromic Substrings for each character in a given String Given a string S of length n, for each character S[i], the task is to find the number of palindromic substrings of length K such that no substring should contain S[i], the task is to return an array A of length n, where A[i] is the count of palindromic substrings of length K which does not include t
9 min read
Count number of distinct substrings of a given length Given a string S of length N consisting of lower-case English alphabets and an integer 'l', find the number of distinct substrings of length 'l' of the given string. Examples: Input : s = "abcbab", l = 2 Output : 4 All distinct sub-strings of length 2 will be {"ab", "bc", "cb", "ba"} Thus, answer eq
6 min read
Frequency of a Substring in a String Given an input string and a pattern, the task is to find the frequency of occurrences of the string pattern in a given string. Examples: Input: pattern = "man", string = "dhimanman"Output: 2 Input: pattern = "nn", string = "Banana"Output: 0 Input: pattern = "aa", string = "aaaaa"Output : 4 Recommend
14 min read