Open In App

Count of increasing substrings in given String

Last Updated : 17 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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>


Output: 

10

 

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads