Number of Good Ways to Split a String - Problem
You are given a string s.
A split is called good if you can split s into two non-empty strings s_left and s_right where their concatenation is equal to s (i.e., s_left + s_right = s) and the number of distinct letters in s_left and s_right is the same.
Return the number of good splits you can make in s.
Input & Output
Example 1 — Multiple Good Splits
$
Input:
s = "aacaba"
›
Output:
2
💡 Note:
Split at index 2: "aa" (1 unique: a) | "caba" (3 unique: a,b,c) → 1≠3. Split at index 3: "aac" (2 unique: a,c) | "aba" (2 unique: a,b) → 2=2 ✓. Split at index 5: "aacab" (3 unique: a,c,b) | "a" (1 unique: a) → 3≠1. Only 1 good split, but checking all positions gives us 2 total good splits.
Example 2 — No Good Splits
$
Input:
s = "abcd"
›
Output:
1
💡 Note:
Split at index 1: "a" (1 unique) | "bcd" (3 unique) → 1≠3. Split at index 2: "ab" (2 unique) | "cd" (2 unique) → 2=2 ✓. Split at index 3: "abc" (3 unique) | "d" (1 unique) → 3≠1. There is 1 good split.
Example 3 — All Same Characters
$
Input:
s = "aaaa"
›
Output:
3
💡 Note:
Any split will have 1 unique character on both sides: "a"|"aaa", "aa"|"aa", "aaa"|"a" all have equal unique counts.
Constraints
- 1 ≤ s.length ≤ 105
- s consists of only lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code