Problem

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:

  • Initially, if any number has less than four digits, it is padded with leading zeros.
  • The ith digit (1 <= i <= 4) of the key is generated by taking the smallest digit among the ith digits of num1, num2, and num3.

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

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16

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

Output: 0

Explanation:

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

  * The `1st` digit of the `key` is `min(0, 0, 1)`.
  * The `2nd` digit of the `key` is `min(0, 0, 0)`.
  * The `3rd` digit of the `key` is `min(0, 1, 0)`.
  * The `4th` digit of the `key` is `min(1, 0, 0)`.

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

Example 2

1
2
3
4

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

Output: 777

Example 3

1
2
3
4

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

Output: 1

Constraints

  • 1 <= num1, num2, num3 <= 9999

Solution

Method 1 – Digit Extraction and Comparison

Intuition

Pad each number to 4 digits with leading zeros, then for each digit position, take the minimum digit among the three numbers. The result is the concatenation of these minimum digits, with leading zeros removed.

Approach

  1. Convert each number to a string, padded to 4 digits with leading zeros.
  2. For each digit position (0 to 3), take the minimum digit among the three numbers at that position.
  3. Concatenate the minimum digits to form the key.
  4. Remove leading zeros from the result (if any), or return ‘0’ if the result is all zeros.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
public:
    int findKey(int num1, int num2, int num3) {
        string s1 = to_string(10000 + num1).substr(1);
        string s2 = to_string(10000 + num2).substr(1);
        string s3 = to_string(10000 + num3).substr(1);
        string res;
        for (int i = 0; i < 4; ++i) {
            res += min({s1[i], s2[i], s3[i]});
        }
        int ans = stoi(res);
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
func findKey(num1, num2, num3 int) int {
    s1 := fmt.Sprintf("%04d", num1)
    s2 := fmt.Sprintf("%04d", num2)
    s3 := fmt.Sprintf("%04d", num3)
    res := ""
    for i := 0; i < 4; i++ {
        m := s1[i]
        if s2[i] < m {
            m = s2[i]
        }
        if s3[i] < m {
            m = s3[i]
        }
        res += string(m)
    }
    ans, _ := strconv.Atoi(strings.TrimLeft(res, "0"))
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public int findKey(int num1, int num2, int num3) {
        String s1 = String.format("%04d", num1);
        String s2 = String.format("%04d", num2);
        String s3 = String.format("%04d", num3);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; ++i) {
            char c = (char)Math.min(s1.charAt(i), Math.min(s2.charAt(i), s3.charAt(i)));
            sb.append(c);
        }
        String res = sb.toString().replaceFirst("^0+", "");
        return res.isEmpty() ? 0 : Integer.parseInt(res);
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    fun findKey(num1: Int, num2: Int, num3: Int): Int {
        val s1 = "%04d".format(num1)
        val s2 = "%04d".format(num2)
        val s3 = "%04d".format(num3)
        val res = StringBuilder()
        for (i in 0..3) {
            res.append(minOf(s1[i], s2[i], s3[i]))
        }
        return res.toString().trimStart('0').ifEmpty { "0" }.toInt()
    }
}
1
2
3
4
5
6
7
class Solution:
    def findKey(self, num1: int, num2: int, num3: int) -> int:
        s1 = f"{num1:04d}"
        s2 = f"{num2:04d}"
        s3 = f"{num3:04d}"
        ans = ''.join(str(min(int(s1[i]), int(s2[i]), int(s3[i]))) for i in range(4))
        return int(ans.lstrip('0') or '0')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
impl Solution {
    pub fn find_key(num1: i32, num2: i32, num3: i32) -> i32 {
        let s1 = format!("{:04}", num1);
        let s2 = format!("{:04}", num2);
        let s3 = format!("{:04}", num3);
        let mut res = String::new();
        for i in 0..4 {
            let m = s1.chars().nth(i).unwrap().min(s2.chars().nth(i).unwrap()).min(s3.chars().nth(i).unwrap());
            res.push(m);
        }
        res.trim_start_matches('0').parse().unwrap_or(0)
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    findKey(num1: number, num2: number, num3: number): number {
        const s1 = num1.toString().padStart(4, '0');
        const s2 = num2.toString().padStart(4, '0');
        const s3 = num3.toString().padStart(4, '0');
        let res = '';
        for (let i = 0; i < 4; ++i) {
            res += Math.min(+s1[i], +s2[i], +s3[i]);
        }
        return parseInt(res.replace(/^0+/, '') || '0');
    }
}

Complexity

  • ⏰ Time complexity: O(1), since the number of digits is fixed (4).
  • 🧺 Space complexity: O(1), as only a constant amount of space is used.