---
# System prepended metadata

title: 'Maximum Time <img src="https://img.shields.io/badge/-easy-brightgreen">'
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Maximum Time <img src="https://img.shields.io/badge/-easy-brightgreen">

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.

**Example 1:**

```
Input: "?4:5?"
Output: "14:59"
```

**Example 2:**

```
Input: "23:5?"
Output: "23:59"
```

**Example 3:**

```
Input: "2?:22"
Output: "23:22"
```
**Example 4:**

```
Input: "0?:??"
Output: "09:59"
```
**Example 5:**

```
Input: "??:??"
Output: "23:59"
```

# Solution

```python=
def maximum_time(s):
    """
    ab:cd
        - a:
            . 1 -> b = 9.
            . 2 -> b = 3.
        - b:
            . [0-3] -> a = 2.
            . [4-9] -> a = 1.
        - c:
            . [0-5] -> d = 9.
        - d:
            . [0-9] -> c = 5.
    """
    if s == "??:??":
        return "23:59"
    else:
        maxTime = ""
        
        # a.
        if s[0] != "?":
            maxTime += s[0]
        elif s[1] in "456789":
            maxTime += "1"
        else:
            maxTime += "2"
        # b.
        if s[1] != "?":
            maxTime += s[1]
        elif s[0] in "01":
            maxTime += "9"
        else:
            maxTime += "3"
        # :.
        maxTime += ":"
        # c.
        maxTime += "5" if s[3] == "?" else s[3]
        # d.
        maxTime += "9" if s[4] == "?" else s[4]
        
        return maxTime

```