You are given three binary strings s1
, s2
, and target
, each of length n
. You need to determine whether it is possible to make all characters in each position of the three strings equal to the corresponding character in the target
string using the following operation:
i
(0 <= i < n
) and select a pair of indices (j, k)
with 0 <= j < k < 3
. Then you can set s[j][i] = s[k][i]
.Return true
if it’s possible to make the three strings equal to the target string, and false
otherwise.
s1
, s2
, and target
have different lengths?
n
.n
(length of the strings)?
n
.Given the functionality of the operation allowed, each position in the strings can be independently analyzed to see if they can be adjusted to match the corresponding position in the target string.
For each position i
:
s1[i]
, s2[i]
, and target[i]
.s1[i]
and s2[i]
, check if it’s feasible to adjust both to match target[i]
.def make_three_strings_equal(s1, s2, target):
n = len(s1)
for i in range(n):
if target[i] == '0':
# To make all zeros, we need both to have at least one zero at position i
if not (s1[i] == '0' or s2[i] == '0'):
return False
elif target[i] == '1':
# To make all ones, we need both to have at least one one at position i
if not (s1[i] == '1' or s2[i] == '1'):
return False
return True
O(n)
, where n
is the length of the strings.O(1)
since we are only using a constant amount of space for variables.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?