Apply Bitwise Operations to Make Strings Equal - Problem

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.

Input & Output

Example 1 — Basic Transformation
$ Input: s = "1010", target = "1100"
Output: true
💡 Note: Choose i=1, j=2: s[1] OR s[2] = 1|1 = 1, s[1] XOR s[2] = 1^1 = 0. Result: "1100"
Example 2 — Impossible Case
$ Input: s = "0000", target = "1100"
Output: false
💡 Note: Since s has no 1s, we cannot create any 1s using OR/XOR operations
Example 3 — Already Equal
$ Input: s = "1111", target = "1111"
Output: true
💡 Note: s already equals target, no operations needed

Constraints

  • 1 ≤ s.length ≤ 105
  • s.length == target.length
  • s and target consist of only '0' and '1'

Visualization

Tap to expand
Bitwise String Transformation ProblemSource: "1010"Binary string to transformTarget: "1100"Desired resultAvailable OperationsChoose indices i ≠ j:s[i] = s[i] OR s[j]s[j] = s[i] XOR s[j]Question: Can we transform s into target?Answer: Check using mathematical properties
Understanding the Visualization
1
Input
Two binary strings s and target of same length
2
Operations
Apply OR and XOR operations on different indices
3
Goal
Check if s can be transformed into target
Key Takeaway
🎯 Key Insight: OR operations can only increase 1s, so strings with no 1s cannot reach targets with 1s
Asked in
Google 15 Meta 12 Microsoft 8
12.5K Views
Medium Frequency
~25 min Avg. Time
387 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen