Count Magical Substring Pairs II Problem
Description
LeetCode Problem 4091.
You are given a string s of length n consisting of lowercase English letters.
For every triplet of indices (i, j, k) satisfying 0 <= i <= j < k < n, define:
s1 = s[i..j]s2 = s[j + 1..k]
A pair of substrings (s1, s2) is called magical if the number of distinct characters in s1 is equal to the number of distinct characters in s2.
Return the number of triplets (i, j, k) for which (s1, s2) is a magical pair.
Example 1:
Input: s = "aba"
Output: 2
Explanation:
There are 2 valid triplets:
No. (i, j, k) s1 s2 distinct(s1) distinct(s2)
1 (0, 0, 1) "a" "b" 1 1
2 (1, 1, 2) "b" "a" 1 1
Example 2:
Input: s = "kkk"
Output: 4
Explanation:
There are 4 valid triplets:
No. (i, j, k) s1 s2 distinct(s1) distinct(s2)
1 (0, 0, 1) "k" "k" 1 1
2 (0, 0, 2) "k" "kk" 1 1
3 (0, 1, 2) "kk" "k" 1 1
4 (1, 1, 2) "k" "k" 1 1
Example 3:
Input: s = "abcac"
Output: 7
Explanation:
There are 7 valid triplets:
No. (i, j, k) s1 s2 distinct(s1) distinct(s2)
1 (0, 0, 1) "a" "b" 1 1
2 (1, 1, 2) "b" "c" 1 1
3 (2, 2, 3) "c" "a" 1 1
4 (3, 3, 4) "a" "c" 1 1
5 (0, 1, 3) "ab" "ca" 2 2
6 (0, 1, 4) "ab" "cac" 2 2
7 (1, 2, 4) "bc" "ac" 2 2
Constraints:
2 <= s.length <= 10^5sconsists of lowercase English letters.
Solution
Approach: Sliding Window per Distinct Count
Let f[r][k] be the length of the longest substring ending at r with at most k distinct characters (and f[r][0] = 0). Every suffix of that substring also has at most k distinct characters, and a shorter suffix has no more distinct characters than a longer one, so exactly f[r][k] - f[r][k-1] substrings ending at r have exactly k distinct characters.
For each k from 1 to 26, one two-pointer pass fills f[.][k]: grow the right end, and while the window has more than k distinct characters move the left end forward.
Run the same computation on the reversed string. A substring of s starting at j + 1 becomes a substring of the reversed string ending at index n - 2 - j. For every split j and every k, multiply the number of left parts and right parts with exactly k distinct characters and add the product.
The Python version gets the same counts without the 26 passes, which are slow there. Keep the last occurrences of the distinct characters seen so far, newest first: p_1 > p_2 > .... A substring ending at r has exactly k distinct characters when it starts in p_(k+1) + 1 .. p_k, so there are p_k - p_(k+1) of them (with p_(m+1) = -1 after the last one). Moving the current character to the front of that short list takes at most 26 steps, done by built-in list operations.
Complexity: O(26 n) time and O(26 n) space. The count can exceed 32 bits, so use a 64-bit integer.
from operator import mul, sub
class Solution:
def calc(self, s: str) -> List[List[int]]:
# f[r][k - 1]: substrings ending at r with exactly k distinct characters.
# With the last occurrences p_1 > p_2 > ... of the distinct characters seen so far,
# the starts with exactly k distinct characters are p_{k+1} + 1 .. p_k.
f = []
last = [] # last occurrences, newest first
pos = {}
for r, c in enumerate(s):
if c in pos:
last.remove(pos[c])
pos[c] = r
last.insert(0, r)
f.append(list(map(sub, last, last[1:] + [-1])))
return f
def countMagicalPairs(self, s: str) -> int:
n = len(s)
left = self.calc(s)
right = self.calc(s[::-1])
ans = 0
for j in range(n - 1):
ans += sum(map(mul, left[j], right[n - 2 - j]))
return ans