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.
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.
Input: s = "abcd"
Output: 1
Explanation: Split the string as follows ("ab", "cd").
Constraints:
s consists of only lowercase English letters.The brute force approach involves using a nested loop to try every possible split point and count unique characters on both sides of the split. While this method is straightforward, it is inefficient because it involves recalculating the unique character count for both substrings at every possible split point.
To improve efficiency, we can use two frequency maps to track character counts on both sides of the split. We start with all characters in the 'right' map, and as we process each character, we move it from the 'right' map to the 'left' map until the split point is found. This allows maintaining constant time complexity for adjusting character counts and only recalculates the number of unique characters at each split.
leftFreq and rightFreq to keep track of character occurrences on both sides.rightFreq array.leftFreq and rightFreq as characters move from right to left.leftUnique and rightUnique to track the number of unique characters in each partition as they evolve.