Given a string which consists of only 0s, 1s or 2s, count the number of substrings that have equal number of 0s, 1s and 2s.
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 Input : str = "102100211" Output : 5
A simple solution is to iterate through all substring of str and checking whether they contain equal 0,1 and 2 or not. Total number of substring of str is O(n2) checking each substring for count takes O(n) times, So total time taken to solve this problem is O(n3) time with brute-force approach.
An efficient solution is to keep track of counts of 0, 1 and 2.
Let zc[i] denotes number of zeros between index 1 and i oc[i] denotes number of ones between index 1 and i tc[i] denotes number of twos between index 1 and i for substring str[i, j] to be counted in result we should have : zc[i] – zc[j-1] = oc[i] – oc[j-1] = tc[i] - tc[j-1] we can write above relation as follows : z[i] – o[i] = z[j-1] – o[j-1] and z[i] – t[i] = z[j-1] – t[j-1]
The above relations can be tracked while looping in string, at each index i we’ll calculate this difference pair and we’ll check how many time this difference pair has previously occurred and we’ll add that count to our result, for keeping track of previous difference pair we have used map in below code. Total time complexity of this solution is O(n log n) considering the fact that map operations, like search and insert take O(Log n) time.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Split the binary string into substrings with equal number of 0s and 1s
- Number of substrings with count of each character as k
- Count of substrings consisting of even number of vowels
- Count number of substrings with numeric value greater than X
- Count number of substrings with exactly k distinct characters
- Count number of distinct substrings of a given length
- Given a binary string, count number of substrings that start and end with 1.
- Count the number of vowels occurring in all the substrings of given string
- Count number of substrings of a string consisting of same characters
- Count number of triplets with product equal to given number
- Count triplets such that sum of any two number is equal to third | Set 2
- Count subarrays with equal number of 1's and 0's
- Count number of equal pairs in a string
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count the number of strings in an array whose distinct characters are less than equal to M
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Split a given string into substrings of length K with equal sum of ASCII values
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Count substrings with same first and last characters