Geeksforgeeks

Error

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:2Explanation: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}

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

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

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

YesLoading...

Equal 0, 1 and 2

...