#1525

Number of Good Ways to Split a String

pupil · 580 · lc medium +29 · verified · 68.4% accepted · 2,118 likes · top 75%

Description

For a string s, a split is good if you can divide s into two non-empty parts sleft and sright such that both parts contain the same number of distinct characters. Return the total count of such good splits.

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").

Code

1
2
3