3270. Find the Key of the Numbers

3270. Find the Key of the Numbers

Description

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 i^th digit (1 <= i <= 4) of the key is generated by taking the smallest digit among the i^th digits of num1, num2, and num3.

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

Example 1:

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

Output: 0

Explanation:

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

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

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

Example 2:

1
2
3
Input: num1 = 987, num2 = 879, num3 = 798

Output: 777

Example 3:

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

Output: 1

Constraints:

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

Hints/Notes

Solution

Language: C++

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int generateKey(int num1, int num2, int num3) {
int exp = 1, res = 0;
for (int i = 0; i < 4; i++) {
int t1 = num1 % 10, t2 = num2 % 10, t3 = num3 % 10;
res = res + exp * (min(t1, min(t2, t3)));
exp *= 10; num1 /= 10; num2 /= 10; num3 /= 10;
}
return res;
}
};