Reputation: 644
I was trying to split a binary string such that each substring has the same number of 1's and 0's. By this I mean, given a string like 0010110010, it can be split into 00101 10010 with both substrings having 2 ones and 3 zeros. Could anyone point me to something similar. Sorry, I don't have any code to share.
Upvotes: 0
Views: 717
Reputation: 109557
Of course we do not solve tasks here.
Upvotes: 1
Reputation:
Say a given string can be split into n substrings that meet the condition, each of which has a 0's and b 1's. Guess what? All the substrings must have the same length (== a + b)! Therefore the search space is very limited:
(int)string.length()/2
. Only divisors without a remainder could be a valid one. For example, you have string 010010010, you can't divide it into 2 substrings and let both substrings have the equal number of 0's and 1's because 9/2=4 R 1.If you only need one solution, the time complexity should be no more than o(n^2)
Upvotes: 2