Equal 0, 1 and 2
Medium Accuracy: 52.72% Submissions: 1502 Points: 4

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.

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 ≤ 105

We are replacing the old Disqus forum with the new Discussions section given below.

to report an issue on this page.

### Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?