For example, there is a given string which is consisted of 1s and 0s:
s = "00000000001111111111100001111111110000";
- What is the efficient way to get the count of longest 1s substring in s? (
11) - What is the efficient way to get the count of longest 0s substring in s? (
10)
I appreciate the question would be answered from an algorithmic perspective.
I think the most straight-forward way is to walk through the bit-string while recording the max lengths for all
0and all1sub-strings. This is ofO (n)complexity as suggested by others.If you can afford some sort of a data-parallel computation, you might want to look at parallel patterns as explained here. Specifically, take a look at parallel reduction. I think this problem can be implemented in
O (log n)time if you can afford one of those methods.I’m trying to think of a parallel reduction for this problem:
On the first level of the reduction, each thread will process chunks of 8 bit strings (depending on the number of threads you have and the length of the string) and produce a summary of the bit string like:
0 -> x, 1 -> y, 0 -> z, ....On the next level each thread will merge two of these summaries into one, any possible joins will be performed at this phase (basically, if the previous summary ended with a
0(1) and the next summary begins with a0(1), then the last entry and the first entry of the two summaries can be collapsed into one).On the top level there will be just one structure with the overall summary of the bit string, which you’ll have to step through to figure out the largest sequences (but this time they are all in summary form, so it should be faster). Or, you can make each summary structure keep track of the larges
0and1sub-strings, this will make it unnecessary to walk through the final structure.I guess this approach only makes sense in a very limited scope, but since you seem to be very keen on getting better than
O (n)…