Similar Problems
Similar Problems not available
Number Of Ways To Split A String  Leetcode Solution
Companies:
LeetCode: Number Of Ways To Split A String Leetcode Solution
Difficulty: Medium
The "Number Of Ways To Split A String" problem on LeetCode asks us to find the number of ways to split a string into three nonempty parts, such that each part contains the same number of distinct characters. In other words, for each position of splitting, we need to check if the number of distinct characters in the left part, center part, and right part are the same.
For example, for the string "abacbc", we can split it into ("a", "ba", "cbc"), ("ab", "a", "cbc"), ("ab", "ac", "bc"), ("aba", "cbc", ""), or ("a", "bac", "bc"), which gives us a total of 5 valid splits.
To solve this problem, we can start by counting the number of distinct characters in the string. If the number of distinct characters is less than 3, then we cannot split the string into three parts with equal number of distinct characters. Otherwise, we can loop over all possible positions to split the string into three parts and check if each part has the same number of distinct characters.
Here is the detailed solution:

Count the number of distinct characters in the string using a set.
def countDistinct(s): return len(set(s)) dist_count = countDistinct(s)

If the number of distinct characters is less than 3, return 0 as we cannot split the string into three parts with equal number of distinct characters.
if dist_count < 3: return 0

Initialize a counter variable
ans
to keep track of the number of valid splits.ans = 0

Loop over all possible positions to split the string into three parts. The range of the loop can be from 1 to n2, where n is the length of the string.
for i in range(1, n1):

Initialize two sets
left
andright
to keep track of the distinct characters in the left and right parts respectively.left = set(s[:i]) right = set(s[i:])

Check if the number of distinct characters in the left and right parts is the same. If not, continue to the next iteration of the loop.
if len(left) != len(right): continue

Loop over all possible positions to split the center part, which can be from j=i+1 to n1i. Initialize a set
center
to keep track of the distinct characters in the center part.for j in range(i+1, ni): center = set(s[i:j])

Check if the number of distinct characters in the center part is the same as the number of distinct characters in the left and right parts. If not, continue to the next iteration of the loop.
if len(center) != len(left): continue

If all three parts have the same number of distinct characters, increment the counter variable
ans
.ans += 1

Return the value of
ans
.
return ans
The time complexity of this solution is O(n^3) as we are looping over all possible positions to split the string and checking the number of distinct characters in each part. However, we can optimize this solution further by precomputing the number of distinct characters in each prefix and suffix of the string, which reduces the time complexity to O(n^2).
Here is the optimized solution:

Count the number of distinct characters in the string using a set.
def countDistinct(s): return len(set(s)) dist_count = countDistinct(s)

If the number of distinct characters is less than 3, return 0 as we cannot split the string into three parts with equal number of distinct characters.
if dist_count < 3: return 0

Initialize a counter variable
ans
to keep track of the number of valid splits.ans = 0

Initialize two lists
left
andright
to keep track of the number of distinct characters in each prefix and suffix respectively.left = [0] * (n+1) right = [0] * (n+1) for i in range(1, n+1): left[i] = left[i1] + (s[i1] not in left_set) right[ni] = right[ni+1] + (s[ni] not in right_set)

Loop over all possible positions to split the string into three parts. The range of the loop can be from 1 to n2, where n is the length of the string.
for i in range(1, n1):

Check if the number of distinct characters in the left and right parts is the same. If not, continue to the next iteration of the loop.
if left[i] != right[i]: continue

Loop over all possible positions to split the center part, which can be from j=i+1 to n1i.
for j in range(i+1, ni):

Check if the number of distinct characters in the center part is the same as the number of distinct characters in the left and right parts. If not, continue to the next iteration of the loop.
if left[j]  left[i] != left[i]: continue

If all three parts have the same number of distinct characters, increment the counter variable
ans
.ans += 1

Return the value of
ans
.
return ans
The time complexity of the optimized solution is O(n^2) as we are precomputing the number of distinct characters in each prefix and suffix of the string and checking it in constant time.
Number Of Ways To Split A String Solution Code
1