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.import kotlin.math.abs
class Solution {
fun isAdjacentDiffAtMostTwo(s: String): Boolean {
for (i in 0..<s.length - 1) {
if (abs(s[i].code - s[i + 1].code) > 2) {
return false
}
}
return true
}
}