给定一个数字字符串str ,任务是计算数字总和等于其长度的子字符串的数量。
例子:
Input: str = “112112”
Output: 6
Explanation:
Substrings “1”, “1”, “11”, “1”, “1”, “11” satisfy the given condition.
Input: str = “1101112”
Output: 12
天真的方法:最简单的解决方案是生成给定字符串的所有子字符串,并为每个子字符串检查其总和是否等于其长度。对于发现为真的每个子字符串,增加计数。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:上述方法可以使用HashMap和不断更新在HashMap中的子串的计数并在结束打印所需数量进行优化。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
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 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;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to count the number of
// subStrings with sum equal to length
static int countSubStrings(String s, int n)
{
int count = 0, sum = 0;
// Stores the count of subStrings
HashMap mp = new HashMap();
mp.put(0, 1);
for(int i = 0; i < n; ++i)
{
// Add character to sum
sum += (s.charAt(i)- '0');
// Add count of subStrings to result
count += mp.containsKey(sum - (i + 1)) == true ?
mp.get(sum - (i + 1)) : 0;
// Increase count of subarrays
if(!mp.containsKey(sum - (i + 1)))
mp.put(sum - (i + 1), 1);
else
mp.put(sum - (i + 1),
mp.get(sum - (i + 1)) + 1);
}
// Return count
return count;
}
// Driver Code
public static void main(String[] args)
{
String str = "112112";
int n = str.length();
System.out.print(countSubStrings(str, n) + "\n");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to implement
# the above approach
from collections import defaultdict
# Function to count the number of
# substrings with sum equal to length
def countSubstrings(s, n):
count, sum = 0, 0
# Stores the count of substrings
mp = defaultdict(lambda : 0)
mp[0] += 1
for i in range(n):
# Add character to sum
sum += ord(s[i]) - ord('0')
# Add count of substrings to result
count += mp[sum - (i + 1)]
# Increase count of subarrays
mp[sum - (i + 1)] += 1
# Return count
return count
# Driver code
str = '112112'
n = len(str)
print(countSubstrings(str, n))
# This code is contributed by Stuti Pathak
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count the number of
// subStrings with sum equal to length
static int countSubStrings(String s, int n)
{
int count = 0, sum = 0;
// Stores the count of subStrings
Dictionary mp = new Dictionary();
mp.Add(0, 1);
for(int i = 0; i < n; ++i)
{
// Add character to sum
sum += (s[i]- '0');
// Add count of subStrings to result
count += mp.ContainsKey(sum - (i + 1)) == true ?
mp[sum - (i + 1)] : 0;
// Increase count of subarrays
if(!mp.ContainsKey(sum - (i + 1)))
mp.Add(sum - (i + 1), 1);
else
mp[sum - (i + 1)] = mp[sum - (i + 1)] + 1;
}
// Return count
return count;
}
// Driver Code
public static void Main(String[] args)
{
String str = "112112";
int n = str.Length;
Console.Write(countSubStrings(str, n) + "\n");
}
}
// This code is contributed by Rohit_ranjan
输出:
6
时间复杂度: O(N)
辅助空间: O(N)