Latest Time You Can Obtain After Replacing Characters


The Latest Time You Can Obtain After Replacing Characters sub-task is applied to an input string, in which the string is represented as a 12-hour format time when the maximum number of characters are replaced by '?'.

In a 12-hour format time, "HH:MM” where HH is an element from the set {00, 01, …, 10, 11} and MM is also an element from the set {00, 01, …, 59}. The earliest possible time is 00:00, and the latest time is 11:59.

Problem Statement

In this problem statement, the goal is to replace all "?" characters in the string s with digits to form the latest valid time in the format "HH:MM".

Example Scenario 1

Input: s="03:??"

Output: 03:59

Replace ? with 5 and 9 to get the latest valid time 03:59.

Example Scenario 2

Input: s="0?:3?"

Output: 09:39

Replace ? with 9 and 9 to get the latest valid time 09:39.

Example Scenario 3

Input: s="1?:22"

Output: 11:22

Replace ? with 1 to get the latest valid time 11:22.

Time Complexity

The time complexity of the Latest Time You Can Obtain After Replacing Characters is O(1).

To solve this problem in various programming languages, use the following methods.
  • Greedy Approach
  • Brute Force Approach

Greedy Approach

Example

Following is the Greedy Approach involves replacing each '?' in the time string with the maximum possible digit that still forms a valid 12-hour format time. This ensures that we get the latest possible time.

#include <iostream>
#include <string>
using namespace std;

string latestTime(string s) {
   if (s[0] == '?') s[0] = (s[1] <= '1' || s[1] == '?') ? '1' : '0';
   if (s[1] == '?') s[1] = (s[0] == '1') ? '1' : '9';
   if (s[3] == '?') s[3] = '5';
   if (s[4] == '?') s[4] = '9';
   return s;
}

int main() {
   string s = "1?:?0";
   cout << "Latest time = " << latestTime(s) << endl;
   return 0;
}
         

Output

Latest time = 11:50
public class LatestTime {
   public static String latestTime(String s) {
      char[] time = s.toCharArray();
      if (time[0] == '?') time[0] = (time[1] <= '1' || time[1] == '?') ? '1' : '0';
      if (time[1] == '?') time[1] = (time[0] == '1') ? '1' : '9';
      if (time[3] == '?') time[3] = '5';
      if (time[4] == '?') time[4] = '9';
      return new String(time);
   }

   public static void main(String[] args) {
      String s = "1?:?0";
      System.out.println("Latest time = " + latestTime(s));
   }
}
         

Output

Latest time = 11:50
def latest_time(s):
   time = list(s)
   if time[0] == '?':
      time[0] = '1' if time[1] <= '1' or time[1] == '?' else '0'
   if time[1] == '?':
      time[1] = '1' if time[0] == '1' else '9'
   if time[3] == '?':
      time[3] = '5'
   if time[4] == '?':
      time[4] = '9'
   return ''.join(time)

s = "1?:?0"
print(f"Latest time = {latest_time(s)}")
         

Output

Latest time = 11:50

Brute Force Approach

Example

The Brute Force Approach is used to generate all possible valid times in the 12-hour format and checking which one matches the given pattern with '?' characters.

#include <iostream>
#include <string>
using namespace std;

bool isValidTime(const string& time) {
   int hours = stoi(time.substr(0, 2));
   int minutes = stoi(time.substr(3, 2));
   return hours >= 0 && hours <= 11 && minutes >= 0 && minutes <= 59;
}

string latestTime(string s) {
   for (int h = 11; h >= 0; --h) {
      for (int m = 59; m >= 0; --m) {
         string hh = (h < 10 ? "0" : "") + to_string(h);
         string mm = (m < 10 ? "0" : "") + to_string(m);
         string candidate = hh + ":" + mm;
         bool match = true;
         for (int i = 0; i < 5; ++i) {
            if (s[i] != '?' && s[i] != candidate[i]) {
               match = false;
               break;
            }
         }
         if (match) return candidate;
      }
   }
   return "";
}

int main() {
   string s = "1?:?0";
   cout << "Latest time = " << latestTime(s) << endl;
   return 0;
}
         

Output

Latest time = 11:50
public class LatestTime {
   public static boolean isValidTime(String time) {
      int hours = Integer.parseInt(time.substring(0, 2));
      int minutes = Integer.parseInt(time.substring(3, 5));
      return hours >= 0 && hours <= 11 && minutes >= 0 && minutes <= 59;
   }

   public static String latestTime(String s) {
      for (int h = 11; h >= 0; --h) {
         for (int m = 59; m >= 0; --m) {
            String hh = (h < 10 ? "0" : "") + h;
            String mm = (m < 10 ? "0" : "") + m;
            String candidate = hh + ":" + mm;
            boolean match = true;
            for (int i = 0; i < 5; ++i) {
               if (s.charAt(i) != '?' && s.charAt(i) != candidate.charAt(i)) {
                  match = false;
                  break;
               }
            }
            if (match) return candidate;
         }
      }
      return "";
   }

   public static void main(String[] args) {
      String s = "1?:?0";
      System.out.println("Latest time = " + latestTime(s));
   }
}
         

Output

Latest time = 11:50
import re

def is_valid_time(time):
   hours, minutes = map(int, time.split(':'))
   return 0 <= hours <= 11 and 0 <= minutes <= 59

def latest_time(s):
   for hours in range(11, -1, -1):
      for minutes in range(59, -1, -1):
         hh = f"{hours:02d}"
         mm = f"{minutes:02d}"
         candidate = f"{hh}:{mm}"
         match = True
         for i in range(5):
            if s[i] != '?' and s[i] != candidate[i]:
               match = False
               break
         if match:
            return candidate
   return ""

s = "1?:?0"
print(f"Latest time: {latest_time(s)}")
         

Output

Latest time = 11:50

Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 18-Jul-2024

0 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements