Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
You are given a string s.
A split is called good if you can split s into two non-empty strings sleft and sright where their concatenation is equal to s (i.e., sleft + sright = s) and the number of distinct letters in sleft and sright is the same.
Return the number of good splits you can make in s.
Example 1:
Input: s = "aacaba"
Output: 2
Explanation: There are 5 ways to split "aacaba" and 2 of them are good.
("a", "acaba") Left string and right string contains 1 and 3 different letters respectively.
("aa", "caba") Left string and right string contains 1 and 3 different letters respectively.
("aac", "aba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aaca", "ba") Left string and right string contains 2 and 2 different letters respectively (good split).
("aacab", "a") Left string and right string contains 3 and 1 different letters respectively.
Example 2:
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Constraints:
1 <= s.length <= 105s consists of only lowercase English letters.Problem summary: You are given a string s. A split is called good if you can split s into two non-empty strings sleft and sright where their concatenation is equal to s (i.e., sleft + sright = s) and the number of distinct letters in sleft and sright is the same. Return the number of good splits you can make in s.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Dynamic Programming · Bit Manipulation
"aacaba"
"abcd"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1525: Number of Good Ways to Split a String
class Solution {
public int numSplits(String s) {
Map<Character, Integer> cnt = new HashMap<>();
for (char c : s.toCharArray()) {
cnt.merge(c, 1, Integer::sum);
}
Set<Character> vis = new HashSet<>();
int ans = 0;
for (char c : s.toCharArray()) {
vis.add(c);
if (cnt.merge(c, -1, Integer::sum) == 0) {
cnt.remove(c);
}
if (vis.size() == cnt.size()) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #1525: Number of Good Ways to Split a String
func numSplits(s string) (ans int) {
cnt := map[rune]int{}
for _, c := range s {
cnt[c]++
}
vis := map[rune]bool{}
for _, c := range s {
vis[c] = true
cnt[c]--
if cnt[c] == 0 {
delete(cnt, c)
}
if len(vis) == len(cnt) {
ans++
}
}
return
}
# Accepted solution for LeetCode #1525: Number of Good Ways to Split a String
class Solution:
def numSplits(self, s: str) -> int:
cnt = Counter(s)
vis = set()
ans = 0
for c in s:
vis.add(c)
cnt[c] -= 1
if cnt[c] == 0:
cnt.pop(c)
ans += len(vis) == len(cnt)
return ans
// Accepted solution for LeetCode #1525: Number of Good Ways to Split a String
struct Solution;
impl Solution {
fn num_splits(s: String) -> i32 {
let mut total = vec![0; 26];
for b in s.bytes() {
total[(b - b'a') as usize] += 1;
}
let mut left = vec![0; 26];
let mut right = total.to_vec();
let mut res = 0;
for b in s.bytes() {
left[(b - b'a') as usize] += 1;
right[(b - b'a') as usize] -= 1;
let mut diff = 0;
for i in 0..26 {
if left[i] > 0 {
diff += 1;
}
if right[i] > 0 {
diff -= 1;
}
}
if diff == 0 {
res += 1;
}
}
res
}
}
#[test]
fn test() {
let s = "aacaba".to_string();
let res = 2;
assert_eq!(Solution::num_splits(s), res);
let s = "abcd".to_string();
let res = 1;
assert_eq!(Solution::num_splits(s), res);
let s = "aaaaa".to_string();
let res = 4;
assert_eq!(Solution::num_splits(s), res);
let s = "acbadbaada".to_string();
let res = 2;
assert_eq!(Solution::num_splits(s), res);
}
// Accepted solution for LeetCode #1525: Number of Good Ways to Split a String
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1525: Number of Good Ways to Split a String
// class Solution {
// public int numSplits(String s) {
// Map<Character, Integer> cnt = new HashMap<>();
// for (char c : s.toCharArray()) {
// cnt.merge(c, 1, Integer::sum);
// }
// Set<Character> vis = new HashSet<>();
// int ans = 0;
// for (char c : s.toCharArray()) {
// vis.add(c);
// if (cnt.merge(c, -1, Integer::sum) == 0) {
// cnt.remove(c);
// }
// if (vis.size() == cnt.size()) {
// ++ans;
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.