LeetCode in Kotlin

3270. Find the Key of the Numbers

Easy

You are given three positive integers num1, num2, and num3.

The key of num1, num2, and num3 is defined as a four-digit number such that:

Return the key of the three numbers without leading zeros (if any).

Example 1:

Input: num1 = 1, num2 = 10, num3 = 1000

Output: 0

Explanation:

On padding, num1 becomes "0001", num2 becomes "0010", and num3 remains "1000".

Hence, the key is "0000", i.e. 0.

Example 2:

Input: num1 = 987, num2 = 879, num3 = 798

Output: 777

Example 3:

Input: num1 = 1, num2 = 2, num3 = 3

Output: 1

Constraints:

Solution

import kotlin.math.min

class Solution {
    fun generateKey(num1: Int, num2: Int, num3: Int): Int {
        val s1 = (
            min(
                num1 / 1000 % 10,
                min(
                    num2 / 1000 % 10,
                    num3 / 1000 % 10,
                ),
            ) * 1000
            )
        val s2 = (
            min(
                num1 / 100 % 10,
                min(
                    num2 / 100 % 10,
                    num3 / 100 % 10,
                ),
            ) * 100
            )
        val s3 =
            (
                min(
                    num1 / 10 % 10,
                    min(
                        num2 / 10 % 10,
                        num3 / 10 % 10,
                    ),
                ) * 10
                )
        val s4 = min(
            num1 % 10,
            min(num2 % 10, num3 % 10),
        )
        return s1 + s2 + s3 + s4
    }
}