Apply Bitwise Operations to Make Strings Equal — LeetCode 2546 Python Solution
- Problem
- #2546
- Pattern
- Bit Manipulation
- 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
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the string |
| Space | O(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.