Easy
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.public class Solution {
public boolean isAdjacentDiffAtMostTwo(String s) {
for (int i = 0; i < s.length() - 1; i++) {
if (Math.abs(s.charAt(i) - s.charAt(i + 1)) > 2) {
return false;
}
}
return true;
}
}