LeetCode solutions

3230. Remove Adjacent Almost-Equal Characters

My accepted Python solution to LeetCode problem 3230, Remove Adjacent Almost-Equal Characters, running in 3ms.

  • Difficulty: Medium
  • Python
  • Runtime 3ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 3ms, memory 17.4MB, accepted 2026-01-02.

python
class Solution:
    def removeAlmostEqualCharacters(self, word: str) -> int:
        def almost_equal(a, b):
            return abs(ord(a) - ord(b)) <= 1
        
        n = len(word)
        count = 0
        i = 0
        
        while i < n - 1:
            if almost_equal(word[i], word[i + 1]):
                # Change word[i+1], then skip to i+2
                count += 1
                i += 2
            else:
                i += 1
        
        return count

Source