1525. Number of Good Ways to Split a String LeetCode Solution
In this guide, you will get 1525. Number of Good Ways to Split a String LeetCode Solution with the best time and space complexity. The solution to Number of Good Ways to Split a String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Number of Good Ways to Split a String solution in C++
- Number of Good Ways to Split a String solution in Java
- Number of Good Ways to Split a String solution in Python
- Additional Resources

Problem Statement of Number of Good Ways to Split a String
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 <= 105
s consists of only lowercase English letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
1525. Number of Good Ways to Split a String LeetCode Solution in C++
class Solution {
public:
int numSplits(string s) {
const int n = s.length();
int ans = 0;
// prefix[i] := the number of unique letters in s[0..i]
vector<int> prefix(n);
// suffix[i] := of unique letters in s[i..n)
vector<int> suffix(n);
unordered_set<int> seen;
for (int i = 0; i < n; ++i) {
seen.insert(s[i]);
prefix[i] = seen.size();
}
seen.clear();
for (int i = n - 1; i >= 0; --i) {
seen.insert(s[i]);
suffix[i] = seen.size();
}
for (int i = 0; i + 1 < n; ++i)
if (prefix[i] == suffix[i + 1])
++ans;
return ans;
}
};
/* code provided by PROGIEZ */
1525. Number of Good Ways to Split a String LeetCode Solution in Java
class Solution {
public int numSplits(String s) {
final int n = s.length();
int ans = 0;
int[] prefix = new int[n];
int[] suffix = new int[n];
Set<Character> seen = new HashSet<>();
for (int i = 0; i < n; ++i) {
seen.add(s.charAt(i));
prefix[i] = seen.size();
}
seen.clear();
for (int i = n - 1; i >= 0; --i) {
seen.add(s.charAt(i));
suffix[i] = seen.size();
}
for (int i = 0; i + 1 < n; ++i)
if (prefix[i] == suffix[i + 1])
++ans;
return ans;
}
}
// code provided by PROGIEZ
1525. Number of Good Ways to Split a String LeetCode Solution in Python
class Solution:
def numSplits(self, s: str) -> int:
n = len(s)
ans = 0
seen = set()
prefix = [0] * n
suffix = [0] * n
for i in range(n):
seen.add(s[i])
prefix[i] = len(seen)
seen.clear()
for i in reversed(range(n)):
seen.add(s[i])
suffix[i] = len(seen)
for i in range(n - 1):
if prefix[i] == suffix[i + 1]:
ans += 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.