Author: Kamil Debowski
Tester: Lewin Gan
Editorialist: Misha Chorniy
Given a string S. Each character of S is a digit ‘0’ or ‘1’. You need to check if all the ‘1’ digits form a single non-empty segment(consecutive subsequence) in the string.
Length of S in this subtask will be not more than 50. If segment of '1’s exists, it should be consecutive. Let’s iterate over left bound of segment and right bound of segment, and check if only '1’s lies inside this segment, and only '0’s are out of the segment. Complexity of such algorithm is: O(S^3)
Define L - leftmost position of digit ‘1’ in S, R - rightmost position of digit ‘1’ in S. Corner case is: when all characters in S are '0’s, then answer is “NO” as the segment from '1’s is empty. If exists at least one digit ‘1’ in S then all characters between L and R must be equal ‘1’, otherwise subsegment from '1’s is not consecutive. How to check it? Using one simple “if”, if(R-L+1 = C), where C is frequency of '1’s in S. Total complexity of this algorithm is O(S).
Setter’s solution can be found here
Tester’s solution can be found here
Please feel free to post comments if anything is not clear to you.