1736. Latest Time by Replacing Hidden Digits LeetCode Solution
In this guide, you will get 1736. Latest Time by Replacing Hidden Digits LeetCode Solution with the best time and space complexity. The solution to Latest Time by Replacing Hidden Digits 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
- Latest Time by Replacing Hidden Digits solution in C++
- Latest Time by Replacing Hidden Digits solution in Java
- Latest Time by Replacing Hidden Digits solution in Python
- Additional Resources

Problem Statement of Latest Time by Replacing Hidden Digits
You are given a string time in the form of hh:mm, where some of the digits in the string are hidden (represented by ?).
The valid times are those inclusively between 00:00 and 23:59.
Return the latest valid time you can get from time by replacing the hidden digits.
Example 1:
Input: time = “2?:?0”
Output: “23:50”
Explanation: The latest hour beginning with the digit ‘2’ is 23 and the latest minute ending with the digit ‘0’ is 50.
Example 2:
Input: time = “0?:3?”
Output: “09:39”
Example 3:
Input: time = “1?:22”
Output: “19:22”
Constraints:
time is in the format hh:mm.
It is guaranteed that you can produce a valid time from the given string.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
1736. Latest Time by Replacing Hidden Digits LeetCode Solution in C++
class Solution {
public:
string maximumTime(string time) {
string ans = time;
if (time[0] == '?')
ans[0] = time[1] == '?' || time[1] < '4' ? '2' : '1';
if (time[1] == '?')
ans[1] = ans[0] == '2' ? '3' : '9';
if (time[3] == '?')
ans[3] = '5';
if (time[4] == '?')
ans[4] = '9';
return ans;
}
};
/* code provided by PROGIEZ */
1736. Latest Time by Replacing Hidden Digits LeetCode Solution in Java
class Solution {
public String maximumTime(String time) {
char[] ans = time.toCharArray();
if (time.charAt(0) == '?')
ans[0] = time.charAt(1) == '?' || time.charAt(1) < '4' ? '2' : '1';
if (time.charAt(1) == '?')
ans[1] = ans[0] == '2' ? '3' : '9';
if (time.charAt(3) == '?')
ans[3] = '5';
if (time.charAt(4) == '?')
ans[4] = '9';
return new String(ans);
}
}
// code provided by PROGIEZ
1736. Latest Time by Replacing Hidden Digits LeetCode Solution in Python
class Solution:
def maximumTime(self, time: str) -> str:
ans = list(time)
if time[0] == '?':
ans[0] = '2' if time[1] == '?' or time[1] < '4' else '1'
if time[1] == '?':
ans[1] = '3' if ans[0] == '2' else '9'
if time[3] == '?':
ans[3] = '5'
if time[4] == '?':
ans[4] = '9'
return ''.join(ans)
# 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.