1207. Mirror-Set Splits

A designer cuts a ribbon of coloured beads, described by the string s, into a left piece and a right piece. A cut is called a mirror split when both pieces use exactly the same set of distinct colours (letters), regardless of how many beads of each colour there are.

A cut at position k with 1 <= k < n gives the left piece s[0..k) and the right piece s[k..n). Return how many cuts are mirror splits. Keep a bitmask of the letters seen in each prefix and in each suffix, and compare the two masks for every cut.

Here 2 <= s.length <= 2 * 10^5 and s contains lowercase letters. The intended cost is O(n) time and O(n) space, or O(1) extra space with letter counts.

Example 1

Input:
s = "abcab"
Output:
0
Explanation:

In every split one side lacks a letter the other has, for example a|bcab leaves b and c only on the right, so the count is 0.

Example 2

Input:
s = "abba"
Output:
1
Explanation:

Only the split ab|ba has the letters a and b on both sides.

Example 3

Input:
s = "aaaa"
Output:
3
Explanation:

Every split has only the letter a on both sides, so all three splits count.

Constraints

2 ≤ s.length ≤ 2 * 105

s contains lowercase letters only

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…