Home > Enterprise >  JavaScript : return maximum possible `HH:MM`
JavaScript : return maximum possible `HH:MM`

Time:09-14

Leetcode problem:

You are given a string that represents time in the format hh:mm. Some of the digits are blank (represented by ?). Fill in ? such that the time represented by this string is the maximum possible. Maximum time: 23:59, minimum time: 00:00. You can assume that input string is always valid.

CodePudding user response:

You can use the replace function ability of String.replace. The second parameter passed in is the position of the ?, so use that to look up the max value for that position in an array.

const maxValues = ["2", "3", ":", "5", "9"];
const maxTime = (time) => time.replace(/\?/g, (s, p, str) => {
  if (p === 1 && str[0] !== '2' && str[0] !== '?' ) {
    return '9';
  }
  return maxValues[p];
});

console.log(maxTime("?1:1?"))
console.log(maxTime("??:??"))
console.log(maxTime("11:11"))
console.log(maxTime("1?:?1"))

CodePudding user response:

maxTime = (time) => {
        const timeArr = time.split(":");
        let hr = timeArr[0];
        let mn = String(timeArr[1]);
        if (mn.includes("?")) {
          const mnArr = mn.split("");
          if (mnArr[0] === "?" && mnArr[1] === "?") {
            mn = "59";
          } else if (mnArr[0] === "?") {
            mn = "5"   mnArr[1];
          } else if (mnArr[1] === "?") {
            const temp = mnArr[0] === "5" ? "9" : "0";
            mn = mnArr[0]   temp;
          }
        }
        if (hr.includes("?")) {
          const hrArr = hr.split("");
          if (hrArr[0] === "?" && hrArr[1] === "?") {
            hr = "23";
          } else if (hrArr[0] === "?") {
            hr = "2"   hrArr[1];
            hr = Number(hr) <= 24 ? "1"   hrArr[1] : hr;
          } else if (hrArr[1] === "?") {
            const temp = hrArr[0] === "2" ? "3" : "9";
            hr = hrArr[0]   temp;
          }
        }

        return `(${time}) => ${hr}:${mn}`;
      }

CodePudding user response:

This is not a very elegant solution but it addresses the problem I pointed out in my reply to James, where the send H in HH can be either 9 or 3 depending on the time. This also is just different but also valid.

maxTime = (strTime) => {
  let [...str] = strTime;
    if (str[0] == "?") { str[0] = "2"; }
    if (str[1] == "?" && str[0] < "2") { str[1] = "9"; } else { str[1] = "3"; }
    if (str[3] == "?") { str[3] = "5"; }
    if (str[4] == "?") { str[4] = "9"; }
    return str.join('');
}

console.log(maxTime("?2:22")); // 22:22
console.log(maxTime("2?:22")); // 23:22
console.log(maxTime("22:?2")); // 22:52
console.log(maxTime("22:2?")); // 22:29
console.log(maxTime("0?:??")); // 09:59
console.log(maxTime("1?:??")); // 19:59
console.log(maxTime("??:??")); // 23:59

or a loop

const max = "29:59";
maxTime = (strTime) => {
  let [...str] = strTime;
  for (x = 0; x < 5; x  ) { if (strTime[x] == "?") { str[x] = max[x]; }}
  if (str[0] == "2" && strTime[1] == "?") { str[1] = "3"; }
  return str.join('');
}

console.log(maxTime("?2:22")); // 22:22
console.log(maxTime("2?:22")); // 23:22
console.log(maxTime("22:?2")); // 22:52
console.log(maxTime("22:2?")); // 22:29
console.log(maxTime("0?:??")); // 09:59
console.log(maxTime("1?:??")); // 19:59
console.log(maxTime("??:??")); // 23:59

  • Related