P16330 [GeeksforGeeks] Equal 0 1 and 23208
普及
题目描述

Given a string str of length N which consists of only 0, 1 or 2s, count the number of substring which have equal number of 0s, 1s and 2s.

Example 1:
Input: str = “0102010”
Output: 2
Explanation: Substring str[2, 4] = “102” and
substring str[4, 6] = “201” has equal number
of 0, 1 and 2
Example 2:
Input: str = “11100022”
Output: 0
Explanation: There is no substring with
equal number of 0 , 1 and 2.
Your Task:
You don't need to read input or print anything.Your task is to complete the function getSubstringWithEqual012() which takes a single string str as input and returns the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}

输入格式

见题面

输出格式

见题面

样例输入
str = “0102010”
样例输出
2