← Home

1525. Number of Good Ways to Split a String

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
binary searchC++Markdown
152

The trick here is to name the state correctly, then let the implementation follow. For 1525. Number of Good Ways to Split a String, the solution in this repository is mainly a binary search solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: binary search, two pointers.

The notes already sitting in the source point us in the right direction:

  • TLE
  • 49 / 61 test cases passed.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are uniqCount, uniqCountInterval, numSplits, leftCounter.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(N)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//TLE
02//49 / 61 test cases passed.
03class Solution {
04public:
05    int uniqCount(string& s, int start, int end){
06        set<char> myset(s.begin()+start, s.begin()+end+1);
07        return myset.size();
08    };
09    
10    int uniqCountInterval(string& s, int l, int r){
11        int count = 0;
12        int n = s.size();
13        
14        if(l > r) return 0;
15        
16        int mid = (l+r) >> 1;
17        
18        if(uniqCount(s, 0, mid) < uniqCount(s, mid+1, n-1)){
19            return uniqCountInterval(s, mid+1, r);
20        }else if(uniqCount(s, 0, mid) > uniqCount(s, mid+1, n-1)){
21            return uniqCountInterval(s, l, mid-1);
22        }else{
23            //uniqCount(s, 0, mid) == uniqCount(s, mid+1, n-1)
24            ++count;
25            count += uniqCountInterval(s, l, mid-1);
26            count += uniqCountInterval(s, mid+1, r);
27        }
28        
29        return count;
30    }
31    
32    int numSplits(string s) {
33        int n = s.size();
34        
35        return uniqCountInterval(s, 0, n-1);
36    }
37};
38
39//Two hashmaps
40//Runtime: 36 ms, faster than 94.96% of C++ online submissions for Number of Good Ways to Split a String.
41//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Number of Good Ways to Split a String.
42//time: O(N)
43class Solution {
44public:
45    int numSplits(string s) {
46        vector<int> leftCounter(26), rightCounter(26);
47        int leftUniqCount = 0, rightUniqCount = 0;
48
49        for(char c : s){
50            if(rightCounter[c-'a'] == 0) ++rightUniqCount;
51            ++rightCounter[c-'a'];
52        }
53        
54        int n = s.size();
55        int ans = 0;
56        
57        //stop at n-2: right substring cannot be empty
58        for(int i = 0; i < n-1; ++i){
59            if(leftCounter[s[i]-'a'] == 0) ++leftUniqCount;
60            if(rightCounter[s[i]-'a'] == 1) --rightUniqCount;
61            if(leftUniqCount == rightUniqCount) ++ans;
62            ++leftCounter[s[i]-'a'];
63            --rightCounter[s[i]-'a'];
64            // cout << leftUniqCount << ", " << rightUniqCount << endl;
65        }
66        
67        return ans;
68    }
69};

Cost

Complexity

Time
O(N)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.