LeetCode 1736: Latest Time by Replacing Hidden Digits

StringGreedy

Problem Description

Explanation:

To solve this problem, we can iterate through the given time string character by character. For each character, we check if it is a question mark ('?'). If it is a question mark, we replace it with the corresponding maximum possible digit based on its position (for hour or minute). If it is a digit, we keep it as is. Finally, we return the modified time string.

Time Complexity:

The time complexity of this solution is O(1) since we are iterating through a fixed-size input string.

Space Complexity:

The space complexity of this solution is O(1) as we are using a constant amount of extra space.

:

Solutions

class Solution {
    public String maximumTime(String time) {
        char[] timeArray = time.toCharArray();
        
        if (timeArray[0] == '?') {
            timeArray[0] = (timeArray[1] <= '3' || timeArray[1] == '?') ? '2' : '1';
        }
        
        if (timeArray[1] == '?') {
            timeArray[1] = (timeArray[0] == '2') ? '3' : '9';
        }
        
        if (timeArray[3] == '?') {
            timeArray[3] = '5';
        }
        
        if (timeArray[4] == '?') {
            timeArray[4] = '9';
        }
        
        return new String(timeArray);
    }
}

Loading editor...