LeetCode in Kotlin

3461. Check If Digits Are Equal in String After Operations I

Easy

You are given a string s consisting of digits. Perform the following operation repeatedly until the string has exactly two digits:

Return true if the final two digits in s are the same; otherwise, return false.

Example 1:

Input: s = “3902”

Output: true

Explanation:

Example 2:

Input: s = “34789”

Output: false

Explanation:

Constraints:

Solution

class Solution {
    fun hasSameDigits(s: String): Boolean {
        val ch = s.toCharArray()
        var k = ch.size - 1
        while (k != 1) {
            for (i in 0..<k) {
                val a = ch[i].code - 48
                val b = ch[i + 1].code - 48
                val d = (a + b) % 10
                val c = (d + '0'.code).toChar()
                ch[i] = c
            }
            k--
        }
        return ch[0] == ch[1]
    }
}