You are given a string s consisting of digits.
Return true if the absolute difference between every pair of adjacent digits is at most 2, otherwise return false.
The absolute difference between a and b is defined as abs(a - b).
Example 1:
Input: s = "132"
Output: true
Explanation:
s[0] and s[1] is abs(1 - 3) = 2.s[1] and s[2] is abs(3 - 2) = 1.Example 2:
Input: s = "129"
Output: false
Explanation:
s[0] and s[1] is abs(1 - 2) = 1.s[1] and s[2] is abs(2 - 9) = 7, which is greater than 2.
Constraints:
2 <= s.length <= 100s consists only of digits.Problem Overview: Given an integer, determine whether every pair of adjacent digits has an absolute difference of exactly 1. If all neighboring digits satisfy this condition, return true; otherwise return false.
Approach 1: Convert to String and Compare Neighbors (O(n) time, O(n) space)
Convert the integer into a string so each digit becomes easy to access by index. Iterate from the first digit to the second-to-last digit and compute the absolute difference between s[i] and s[i+1]. If any pair has a difference other than 1, return false immediately. This approach is simple and readable, which makes it common in interview settings where clarity matters more than micro-optimizations. The tradeoff is the extra space used to store the string representation.
Approach 2: Mathematical Digit Extraction (O(n) time, O(1) space)
Instead of converting the number to a string, repeatedly extract digits using modulo and division operations. Store the last digit with num % 10, remove it with num // 10, and compare it with the next extracted digit. Each comparison checks abs(current - next) == 1. This avoids allocating extra memory and works directly on the integer. The logic relies on basic math operations and sequential processing similar to techniques used in number manipulation problems.
Approach 3: Early Exit Linear Scan (O(n) time, O(1) space)
Regardless of whether digits come from a string or arithmetic extraction, the optimal strategy is a single pass with early termination. As soon as one pair violates the rule, stop processing and return false. This keeps the algorithm efficient and avoids unnecessary work. The pattern is essentially a simple linear scan where each step compares neighboring elements.
Recommended for interviews: The mathematical digit extraction approach. It demonstrates comfort with integer manipulation and achieves O(1) auxiliary space. The string approach is still acceptable and often faster to implement during a timed interview, but showing the constant‑space method signals stronger control over low‑level operations.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Conversion and Neighbor Check | O(n) | O(n) | When readability and quick implementation matter |
| Mathematical Digit Extraction | O(n) | O(1) | When optimizing memory or demonstrating number manipulation |
| Early Exit Linear Scan | O(n) | O(1) | General case where immediate failure detection improves runtime |
Practice Check Adjacent Digit Differences with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor