[LeetCode] Check Adjacent Digit Differences

3931. Check Adjacent Digit Differences

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).

1
2
3
4
5
6
7
8
9
class Solution {
public:
bool isAdjacentDiffAtMostTwo(string s) {
for(int i = 1; i < s.length(); i++) {
if(abs(s[i] - s[i-1]) > 2) return false;
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/check-adjacent-digit-differences/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.