3114. Latest Time You Can Obtain After Replacing Characters LeetCode Solution

In this guide, you will get 3114. Latest Time You Can Obtain After Replacing Characters LeetCode Solution with the best time and space complexity. The solution to Latest Time You Can Obtain After Replacing Characters 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

  1. Problem Statement
  2. Complexity Analysis
  3. Latest Time You Can Obtain After Replacing Characters solution in C++
  4. Latest Time You Can Obtain After Replacing Characters solution in Java
  5. Latest Time You Can Obtain After Replacing Characters solution in Python
  6. Additional Resources
3114. Latest Time You Can Obtain After Replacing Characters LeetCode Solution image

Problem Statement of Latest Time You Can Obtain After Replacing Characters

You are given a string s representing a 12-hour format time where some of the digits (possibly none) are replaced with a “?”.
12-hour times are formatted as “HH:MM”, where HH is between 00 and 11, and MM is between 00 and 59. The earliest 12-hour time is 00:00, and the latest is 11:59.
You have to replace all the “?” characters in s with digits such that the time we obtain by the resulting string is a valid 12-hour format time and is the latest possible.
Return the resulting string.

Example 1:

Input: s = “1?:?4”
Output: “11:54”
Explanation: The latest 12-hour format time we can achieve by replacing “?” characters is “11:54”.

Example 2:

Input: s = “0?:5?”
Output: “09:59”
Explanation: The latest 12-hour format time we can achieve by replacing “?” characters is “09:59”.

See also  1144. Decrease Elements To Make Array Zigzag LeetCode Solution

Constraints:

s.length == 5
s[2] is equal to the character “:”.
All characters except s[2] are digits or “?” characters.
The input is generated such that there is at least one time between “00:00” and “11:59” that you can obtain after replacing the “?” characters.

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

3114. Latest Time You Can Obtain After Replacing Characters LeetCode Solution in C++

class Solution {
 public:
  // Similar to 1736. Latest Time by Replacing Hidden Digits
  string findLatestTime(string s) {
    string ans = s;
    if (s[0] == '?')
      ans[0] = s[1] == '?' || s[1] < '2' ? '1' : '0';
    if (s[1] == '?')
      ans[1] = ans[0] == '1' ? '1' : '9';
    if (s[3] == '?')
      ans[3] = '5';
    if (s[4] == '?')
      ans[4] = '9';
    return ans;
  }
};
/* code provided by PROGIEZ */

3114. Latest Time You Can Obtain After Replacing Characters LeetCode Solution in Java

class Solution {
  // Similar to 1736. Latest Time by Replacing Hidden Digits
  public String findLatestTime(String s) {
    char[] ans = s.toCharArray();
    if (s.charAt(0) == '?')
      ans[0] = s.charAt(1) == '?' || s.charAt(1) < '2' ? '1' : '0';
    if (s.charAt(1) == '?')
      ans[1] = ans[0] == '1' ? '1' : '9';
    if (s.charAt(3) == '?')
      ans[3] = '5';
    if (s.charAt(4) == '?')
      ans[4] = '9';
    return new String(ans);
  }
}
// code provided by PROGIEZ

3114. Latest Time You Can Obtain After Replacing Characters LeetCode Solution in Python

class Solution:
  # Similar to 1736. Latest Time by Replacing Hidden Digits
  def findLatestTime(self, s: str) -> str:
    ans = list(s)
    if s[0] == '?':
      ans[0] = '1' if s[1] == '?' or s[1] < '2' else '0'
    if s[1] == '?':
      ans[1] = '1' if ans[0] == '1' else '9'
    if s[3] == '?':
      ans[3] = '5'
    if s[4] == '?':
      ans[4] = '9'
    return ''.join(ans)
# code by PROGIEZ

Additional Resources

See also  2423. Remove Letter To Equalize Frequency LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.