MediumNo. 2546
Apply Bitwise Operations to Make Strings Equal — LeetCode 2546 Python Solution
- Pattern
- Bit Manipulation
- Topics
- Bit Manipulation, String
- Reading time
- 2 min
- Source
- leetcode.com
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
class Solution:
def makeStringsEqual(self, s: str, target: str) -> bool:
return ("1" in s) == ("1" in target)Complexity
- Time
- O(n)where n is the length of the string
- Space · auxiliary
- O(1)
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
| No. | Problem | Difficulty |
|---|---|---|
| 0067 | Add Binary | Easy |
| 0318 | Maximum Product of Word Lengths | Medium |
| 0405 | Convert a Number to Hexadecimal | Easy |
| 1404 | Number of Steps to Reduce a Number in Binary Representation to One | Medium |
| 0029 | Divide Two Integers | Medium |
| 0136 | Single Number | Easy |
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.