↓Skip to main content
  1. LeetCode/

LeetCode 3931: Check Adjacent Digit Differences

·1 min· ·
LeetCode Easy String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-05-17

  • Submissions: Accepted (first try)

Approach
#

Convert characters to digits and compare each adjacent pair once (String Template 4). Any pair differing by more than 2 means False.

My Solution
#

class Solution:
    def isAdjacentDiffAtMostTwo(self, s: str) -> bool:
        return all(abs(int(a) - int(b)) <= 2 for a, b in zip(s, s[1:]))

Complexity
#

Time O(n), space O(n) (the s[1:] copy).

Mistakes to Avoid
#

  • Convert characters to digits before subtracting.

Interview Explanation
#

I pair the string with itself shifted by one and return True when every adjacent pair differs by at most 2. Time O(n).

Related

LeetCode 3884: First Matching Character From Both Ends
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 3884 note: compare from both ends toward the middle; the first match is the answer, and odd lengths always match in the middle
LeetCode 3838: Weighted Word Mapping
·1 min
LeetCode Easy Array String Simulation
LeetCode 3838 note: sum each word’s letter weights mod 26 and map backward: 0 -> z, i.e. chr(ord(‘z’) - r)
LeetCode 3823: Reverse Letters Then Special Characters in a String
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 3823 note: two converging-pointer passes, swapping only letters the first time and only special characters the second
LeetCode 2273: Find Resultant Array After Removing Anagrams
·2 mins
LeetCode Easy Hash-Table String Sorting
LeetCode 2273 note: repeatedly deleting a word that is an anagram of the previous one keeps exactly the words that are not anagrams of the last kept word; compare sorted strings
LeetCode 2697: Lexicographically Smallest Palindrome
·2 mins
LeetCode Easy Two-Pointers String Greedy
LeetCode 2697 note: converging pointers, setting both ends to the smaller character whenever they differ
LeetCode 3258: Count Substrings That Satisfy K-Constraint I
·2 mins
LeetCode Easy Sliding-Window String
LeetCode 3258 note: valid means zeros <= k or ones <= k; it is invalid only when both exceed k, and after shrinking there are right - left + 1 valid substrings ending at right