Description
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Medium

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 (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)s1s2distinct(s1)distinct(s2)
1(0, 0, 1)"a""b"11
2(1, 1, 2)"b""a"11

Example 2:

Input: s = "kkk"

Output: 4

Explanation:

There are 4 valid triplets:

No.(i, j, k)s1s2distinct(s1)distinct(s2)
1(0, 0, 1)"k""k"11
2(0, 0, 2)"k""kk"11
3(0, 1, 2)"kk""k"11
4(1, 1, 2)"k""k"11

Example 3:

Input: s = "abcac"

Output: 7

Explanation:

There are 7 valid triplets:

No.(i, j, k)s1s2distinct(s1)distinct(s2)
1(0, 0, 1)"a""b"11
2(1, 1, 2)"b""c"11
3(2, 2, 3)"c""a"11
4(3, 3, 4)"a""c"11
5(0, 1, 3)"ab""ca"22
6(0, 1, 4)"ab""cac"22
7(1, 2, 4)"bc""ac"22

 

Constraints:

  • 2 <= s.length <= 2000
  • s consists of lowercase English letters.
 
Code
Code
Testcase
Testcase
Test Result
Test Result