Apply Bitwise Operations to Make Strings Equal — LeetCode 2546 Python Solution

MediumBit ManipulationString
Problem
#2546
Reading time
2 min

The 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.

Example

Input
s = "1010", target = "0110"
Output
true
Explanation
We can do the following operations:

Python solution

Python
class Solution:
    def makeStringsEqual(self, s: str, target: str) -> bool:
        return ("1" in s) == ("1" in target)

Complexity

MeasureComplexity
TimeO(n), where n is the length of the string
SpaceO(1) auxiliary

Pattern: Bit Manipulation

Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 2546. Apply Bitwise Operations to Make Strings Equal is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Bit Manipulation.

The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2546. Apply Bitwise Operations to Make Strings Equal?
LeetCode 2546. Apply Bitwise Operations to Make Strings Equal is rated Medium on LeetCode.
What is the time complexity of LeetCode 2546. Apply Bitwise Operations to Make Strings Equal?
The Python solution on this page runs in O(n), where n is the length of the string.
What is the space complexity of LeetCode 2546. Apply Bitwise Operations to Make Strings Equal?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2546. Apply Bitwise Operations to Make Strings Equal cover?
LeetCode 2546. Apply Bitwise Operations to Make Strings Equal is tagged Bit Manipulation and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview