LeetCode Entry
2839. Check if Strings Can be Made Equal With Operations I
Match a and b at (0|2,1|3) positions
2839. Check if Strings Can be Made Equal With Operations I easy youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1312
Problem TLDR
| Match a and b at (0 | 2,1 | 3) positions #easy |
Intuition
How short can the code be?
Approach
- Kotlin: setOf, (0..1)
- Rust: bitmask
Complexity
-
Time complexity: \(O(1)\)
-
Space complexity: \(O(1)\)
Code
// 24ms
fun canBeEqual(a: String, b: String) =
(0..1).all { setOf(a[it],a[it+2]) == setOf(b[it],b[it+2]) }
// 0ms
pub fn can_be_equal(a: String, b: String) -> bool {
let f = |s: &[u8]| 1<<s[0] | 1<<s[2] | 1<<s[1]+16 | 1<<s[3]+16;
f(a.as_bytes()) == f(b.as_bytes())
}