3270. Find the Key of the Numbers LeetCode Solution
In this guide, you will get 3270. Find the Key of the Numbers LeetCode Solution with the best time and space complexity. The solution to Find the Key of the Numbers problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the Key of the Numbers solution in C++
- Find the Key of the Numbers solution in Java
- Find the Key of the Numbers solution in Python
- Additional Resources
Problem Statement of Find the Key of the Numbers
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).
Example 1:
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:
Input: num1 = 987, num2 = 879, num3 = 798
Output: 777
Example 3:
Input: num1 = 1, num2 = 2, num3 = 3
Output: 1
Constraints:
1 <= num1, num2, num3 <= 9999
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
3270. Find the Key of the Numbers LeetCode Solution in C++
class Solution {
public:
int generateKey(int num1, int num2, int num3) {
const string s1 = zfill(num1, 4);
const string s2 = zfill(num2, 4);
const string s3 = zfill(num3, 4);
string ans;
for (int i = 0; i < 4; ++i)
ans += min({s1[i], s2[i], s3[i]});
return stoi(ans);
}
private:
string zfill(int num, int width) {
ostringstream oss;
oss.width(width);
oss.fill('0');
oss << num;
return oss.str();
}
};
/* code provided by PROGIEZ */
3270. Find the Key of the Numbers LeetCode Solution in Java
class Solution {
public int generateKey(int num1, int num2, int num3) {
final String s1 = String.format("%04d", num1);
final String s2 = String.format("%04d", num2);
final String s3 = String.format("%04d", num3);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; ++i)
sb.append((char) Math.min(Math.min(s1.charAt(i), s2.charAt(i)), s3.charAt(i)));
return Integer.parseInt(sb.toString());
}
}
// code provided by PROGIEZ
3270. Find the Key of the Numbers LeetCode Solution in Python
class Solution:
def generateKey(self, num1: int, num2: int, num3: int) -> int:
return int(''.join(min(a, b, c)
for a, b, c in zip(str(num1).zfill(4),
str(num2).zfill(4),
str(num3).zfill(4))))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.