[LeetCode] Apply Bitwise Operations to Make Strings Equal

2546. Apply Bitwise Operations to Make Strings Equal

You are given two 0-indexed binary strings s and target of the same length n. You can do the following operation on s any number of times:

  • Choose two different indices i and j where 0 <= i, j < n.
  • Simultaneously, replace s[i] with (s[i] OR s[j]) and s[j] with (s[i] XOR s[j]).

For example, if s = “0110”, you can choose i = 0 and j = 2, then simultaneously replace s[0] with (s[0] OR s[2] = 0 OR 1 = 1), and s[2] with (s[0] XOR s[2] = 0 XOR 1 = 1), so we will have s = “1110”.

Return true if you can make the string s equal to target, or false otherwise.

1
2
3
4
5
6
7
8
class Solution {
public:
bool makeStringsEqual(string s, string target) {
if(count(begin(target), end(target), '0') == target.length()) return s == target;
if(count(begin(s), end(s), '0') == s.length()) return false;
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/02/02/PS/LeetCode/apply-bitwise-operations-to-make-strings-equal/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.