-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_CountDigitGroupingsOfANumber.cpp
58 lines (48 loc) · 1.16 KB
/
GFG_CountDigitGroupingsOfANumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
https://practice.geeksforgeeks.org/problems/count-digit-groupings-of-a-number1520/1/#
Count digit groupings of a number
Variation of Palindromic Partition
*/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
int TotalCount(string str){
vector<vector<int>> memo(str.length(), vector<int>(901, -1));
return solve(0, 0, str, memo);
}
int solve(int idx, int prevSum, string &s, vector<vector<int>>& memo)
{
if(idx == s.length())
return 1;
if(memo[idx][prevSum] != -1)
return memo[idx][prevSum];
int cnt=0;
int curSum=0;
for(int i=idx; i<s.length(); i++)
{
curSum+= s[i]-'0';
if(prevSum<= curSum)
{
cnt+= solve(i+1, curSum, s, memo);
}
}
memo[idx][prevSum] = cnt;
return cnt;
}
};
// { Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
string str;
cin >> str;
Solution ob;
int ans = ob.TotalCount(str);
cout << ans <<"\n";
}
return 0;
} // } Driver Code Ends