Problem · String

Ways to Split String

Learn this problem
MediumGoogleFULLTIMEOA
See Google hiring insights

Problem statement

Given a string S, we can split S into 2 strings: S1 and S2. Return the number of ways S can be split such that the number of unique characters between S1 and S2 are the same.

Function

numWaysToSplitString(s: String) → int

Examples

Example 1

s = "aaaa"return = 3
We can get a - aaa, aa - aa, aaa - a.

Example 2

s = "bac"return = 0
There are no ways to split the string such that the number of unique characters between S1 and S2 are the same.

Example 3

s = "ababa"return = 2
We can get ab - aba, aba - ba.

Constraints

  • 2 ≤ s.length ≤ 2 * 10^5
  • s contains ASCII letters.
  • Both resulting substrings must be non-empty.

More Google problems

drafts saved locally
public int numWaysToSplitString(String s) {
    // write your code here
}
s"aaaa"
expected3
checking account