Skip to main content

Check Adjacent Digit Differences - Solution & Explanation

EasyString5 min read
Practice this problem

Problem Statement

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:

  • The absolute difference between digits at s[0] and s[1] is abs(1 - 3) = 2.
  • The absolute difference between digits at s[1] and s[2] is abs(3 - 2) = 1.
  • Since both differences are at most 2, the answer is true.

Example 2:

Input: s = "129"

Output: false

Explanation:

  • The absolute difference between digits at s[0] and s[1] is abs(1 - 2) = 1.
  • The absolute difference between digits at s[1] and s[2] is abs(2 - 9) = 7, which is greater than 2.
  • Therefore, the answer is false.

 

Constraints:

  • 2 <= s.length <= 100
  • s consists only of digits.

Approach Overview

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.

Solution

We can simulate the process described in the problem: iterate through each pair of adjacent digits in the string and compute their absolute difference. If any pair has an absolute difference greater than 2, return false. If no such pair is found after the traversal, return true.

The time complexity is O(n), where n is the length of the string. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Conversion and Neighbor CheckO(n)O(n)When readability and quick implementation matter
Mathematical Digit ExtractionO(n)O(1)When optimizing memory or demonstrating number manipulation
Early Exit Linear ScanO(n)O(1)General case where immediate failure detection improves runtime

Video Solution

Weekly Contest 502 | leetcode 3931|leetcode 3932 | leetcode 3933 | leetcode 3934 | Binary Search • Code With Vick • 841 views views

Watch 7 more video solutions →

Frequently Asked Questions

Is Check Adjacent Digit Differences easy or hard?
Check Adjacent Digit Differences is generally classified as an Easy problem. The logic requires a basic linear scan and simple arithmetic checks between adjacent digits, making it suitable for beginners practicing number manipulation.
Check Adjacent Digit Differences Python/Java solution
The implementation usually iterates through digits and checks abs(digit[i] - digit[i+1]) == 1. Python can iterate over a string or use modulo operations, while Java commonly uses integer division and modulo to extract digits. Both implementations run in O(n) time.
How to solve Check Adjacent Digit Differences in O(n)?
Process digits sequentially and compare every adjacent pair. Extract digits using modulo (num % 10) and integer division (num // 10) or iterate through a string representation. For each pair, check if abs(d1 - d2) equals 1 and stop immediately if the condition fails.
What is the best approach for Check Adjacent Digit Differences?
The best approach is a single linear scan of the digits while checking the absolute difference between adjacent pairs. Extract digits either by converting the number to a string or using modulo and division operations. The optimal implementation runs in O(n) time with O(1) extra space when digits are processed mathematically.
Is Check Adjacent Digit Differences asked at Google/Amazon/Meta?
Problems involving digit manipulation and adjacency checks appear frequently in coding interviews at large tech companies. While this exact problem may vary, similar questions testing number processing, absolute differences, and linear scans show up in screening rounds.
What data structure is used in Check Adjacent Digit Differences?
No complex data structure is required. The solution typically uses simple integer arithmetic or a string representation of the number. The algorithm relies on sequential comparison of digits rather than storing large structures.
What is the time complexity of Check Adjacent Digit Differences?
The time complexity is O(n), where n is the number of digits in the input number. Each digit is processed once while comparing it with its adjacent neighbor. The space complexity can be O(1) with arithmetic extraction or O(n) if the number is converted to a string.

Ready to solve this problem?

Practice Check Adjacent Digit Differences with our built-in code editor and test cases.

Practice on FleetCode