Problem Description
Given three positive integers num1, num2, and num3, each is first represented as a four-digit string (padding with leading zeros if necessary). The key is a four-digit string where the iᵗʰ digit is the smallest among the iᵗʰ digits of the padded representations of num1, num2, and num3. The output is the key number without any leading zeros (return 0 if the result is "0000").
Key Insights
- First, normalize each number to a four-digit string by padding with zeros.
- Compare the corresponding characters (digits) of the three strings.
- Construct a new string made up of the minimum digit from each corresponding digit.
- Convert the resulting string to an integer to eliminate any leading zeros.
- This process is simple and runs in constant time since the operations are fixed (only 4 digits).
Space and Time Complexity
Time Complexity: O(1) – Operations are performed on a fixed-length string (4 digits). Space Complexity: O(1) – Only a few extra string variables are used.
Solution
The solution involves the following steps:
- Convert each number to a four-character string using leading zero padding if required.
- Iterate from index 0 to 3, and for each index, compare the corresponding digits from each string.
- Build the key string using the minimum of the three digits at each position.
- Convert the key string to an integer to strip any leading zeros and return the result. The main data structure used is a string for each padded number, and the built-in string manipulation functions are leveraged to construct the key.