#C11331. Longest Contiguous Ones Surrounded by Zeros
Longest Contiguous Ones Surrounded by Zeros
Longest Contiguous Ones Surrounded by Zeros
Given a binary string s
consisting of characters '0' and '1', your task is to find the length of the longest contiguous subarray of 1s that is strictly surrounded by 0s on both sides. The subarray is only considered valid if there is a '0' immediately before the sequence and a '0' immediately after the sequence. If no such subarray exists, output 0.
Note: A sequence at the beginning or end of the string that does not have a 0 on the corresponding side is not considered surrounded by 0s.
For example:
- For
s = "0101010"
, the answer is 1. - For
s = "110111011"
, the answer is 3. - For
s = "0000000"
, the answer is 0. - For
s = "1111"
, the answer is 0. - For
s = "1010110101"
, the answer is 2.
inputFormat
The input is given through stdin and consists of multiple test cases. The first line contains a single integer T representing the number of test cases. Each of the following T lines contains a binary string s
that consists only of characters '0' and '1'.
Constraints:
- 1 ≤ T ≤ 105
- 1 ≤ |s| ≤ 105 for each test case
outputFormat
For each test case, output a single integer in a separate line which denotes the length of the longest contiguous sequence of 1s that is surrounded by 0s on both sides. The result should be printed to stdout.
## sample5
0101010
110111011
0000000
1111
1010110101
1
3
0
0
2
</p>