Skip to main content
Back to problems
#2546
Medium Algorithms

Apply bitwise operations to make strings equal

String Bit Manipulation
42.6% acceptance
Feb 25, 2026
262
101
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]). Return true if you can make the string s equal to target, or false otherwise.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn make_strings_equal(s: String, target: String) -> bool {
    // Key insight: if s has any '1', we can reach any target with '1'.
    // If s has no '1', we can only reach target with no '1' (i.e., s == target == all zeros).
    // So: reachable iff (s contains '1') == (target contains '1')
    let has1_s = s.contains('1');
    let has1_t = target.contains('1');
    has1_s == has1_t
  }
}