Number of Good Ways to Split a String

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.

Example 1
Inputs = "aacaba"
Output2
There are 5 ways to split "aacaba", and the splits ("aac", "aba") and ("aaca", "ba") have the same number of distinct letters on both sides.
Example 2
Inputs = "abcd"
Output1
Splitting the string as ("ab", "cd") gives two distinct letters on each side.

Constraints

  • 1 <= s.length <= 10^5
  • s consists of only lowercase English letters.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate