LeetCode in Kotlin

3931. Check Adjacent Digit Differences

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:

Example 2:

Input: s = “129”

Output: false

Explanation:

Constraints:

Solution

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
    }
}