LeetCode Entry

2840. Check if Strings Can be Made Equal With Operations II

30.03.2026 medium 2026 kotlin rust

Strings equal at odd-even positions

2840. Check if Strings Can be Made Equal With Operations II medium

youtube

30.03.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1313

Problem TLDR

Strings equal at odd-even positions #medium #hash

Intuition

  1. compare frequencies
  2. compare hashes

Approach

  • as well as it is green its all fine! (for golf)
  • sum((c parity)^4) is the perfect hash

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code

// 49ms
    fun checkStrings(a: String, b: String) = a.indices.sumOf { i ->
        (a[i]-' '+i%2*32.0).pow(3) - (b[i]-' '+i%2*32.0).pow(3)
    } == 0.0
// 0ms
    pub fn check_strings(a: String, b: String) -> bool {
        let h = |s: &[u8]| (0..s.len()).map(|i|
            1<<(s[i] as usize & 31 | i%2*7) ).sum::<usize>();
        h(a.as_bytes()) == h(b.as_bytes())
    }