# 2546. Apply Bitwise Operations to Make Strings Equal ###### tags: `Leetcode` `Medium` `Bit Manipulation` ## 思路 经过列举可以发现只有如下这几种pattern 那么我们可以发现如果s只有0没有1 无论怎么样也无法产生一个1 同理如果target只有0没有1 但是s里面有1 无论怎么样也无法把s里面的1消掉 所以s和target能equal的一个条件是 要么都有1 要么都没有1 但这样是否能保证两个string可以equal呢 答案是可以的 因为只要有1 我们就可以通过下列pattern给s加一个1 或者减一个1 因此只要检查是不是都有或者都没有1即可 0 1 -> 1 1 1 0 -> 1 1 1 1 -> 1 0 / 0 1 0 0 -> 0 0 ## Code ```python= class Solution: def makeStringsEqual(self, s: str, target: str) -> bool: return ('1' in s) == ('1' in target) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up