#C5646. Flip Exactly One Substring
Flip Exactly One Substring
Flip Exactly One Substring
You are given two binary strings, \(S\) and \(T\). Your task is to determine if it is possible to transform \(S\) into \(T\) by flipping (i.e. inverting all bits in) exactly one contiguous substring of \(S\).
Note: Flipping means converting every '0' in the chosen substring to '1' and every '1' to '0'. For the transformation to be valid, the following conditions must hold:
- \(S \neq T\) initially, as no flip is needed if they are already equal.
- Let \(i\) be the first index and \(j\) the last index at which \(S\) and \(T\) differ. If flipping the substring \(S[i \ldots j]\) yields \(T[i \ldots j]\) (i.e. for every \(k\) between \(i\) and \(j\), \(T[k]\) must be the complement of \(S[k]\)), then the answer is
YES
; otherwise, the answer isNO
.
This problem requires careful handling of indices and conditions.
inputFormat
The input consists of exactly two lines:
- The first line contains the binary string \(S\).
- The second line contains the binary string \(T\).
Both strings will have the same length.
outputFormat
Output a single line containing either YES
if \(S\) can be transformed into \(T\) by flipping exactly one contiguous substring, or NO
otherwise.
1100
0010
YES