Count of Substrings having Sum equal to their Length

Count of Substrings having Sum equal to their Length

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Given a numeric string str, the task is to calculate the number of substrings with the sum of digits equal to their length.

Examples:

_Input: __str = “112112” _

_Output: __6 _

Explanation:

Substrings “1”, “1”, “11”, “1”, “1”, “11” satisfy the given condition.

Input:_ str = “1101112” _

_Output: _12

Recommended: Please try your approach on _{IDE}_ first, before moving on to the solution.

*Naive Approach: *The simplest solution is to generate all substrings of the given string and for each substring, check if its sum is equal to its length or not. For each substring found to be true, increase count. 

_Time Complexity: __O(N3) _

Auxiliary Space:_ O(1)_

Efficient Approach: *The above approach can be optimized using a Hashmap and keep updating the count of substrings in the *Hashmap and print the required count at the end.

Below is the implementation of the above approach:

  • C++
  • Java
  • Python3

// C++ Program to implement

// the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to count the number of

// substrings with sum equal to length

**int** countSubstrings(string s, **int** n)

{

**int** count = 0, sum = 0;

// Stores the count of substrings

unordered_map<``**int**``, **int**``> mp;

mp[0]++;

**for** (``**int** i = 0; i < n; ++i) {

// Add character to sum

sum += (s[i] - '0'``);

// Add count of substrings to result

count += mp[sum - (i + 1)];

// Increase count of subarrays

++mp[sum - (i + 1)];

}

// Return count

**return** count;

}

// Driver Code

**int** main()

{

string str = "112112"``;

**int** n = str.length();

cout << countSubstrings(str, n) << endl;

**return** 0;

}

Output: 

6

*Time Complexity: *O(N) 

Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

greedy hash mathematical searching strings cpp-map frequency-counting substring

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Count of Substrings with at least K pairwise Distinct Characters

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Count of Distinct Substrings occurring consecutively in a given String

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Count of distinct Strings possible by swapping prefixes of pairs of Strings

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Smallest String consisting of a String S exactly K times as a Substring

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.