> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Largest Time for Given Digits Python Solution

> Tested Python solution for LeetCode 949 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 949, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [String](/catalog/topics/string), [Backtracking](/catalog/topics/backtracking), [Enumeration](/catalog/topics/enumeration). [View on LeetCode](https://leetcode.com/problems/largest-time-for-given-digits/description/).

Generate this problem as a practice environment: tested reference solution, 21 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 949   # by problem number
lcpy gen -s largest_time_for_given_digits   # by problem name
```

## Problem

Given an array `arr` of 4 digits, find the latest 24-hour time that can be made using each digit exactly once.

24-hour times are formatted as `"HH:MM"`, where `HH` is between `00` and `23`, and `MM` is between `00` and `59`. The earliest 24-hour time is `00:00`, and the latest is `23:59`.

Return the latest 24-hour time in `"HH:MM"` format. If no valid time can be made, return an empty string.

### Examples

```
Input: arr = [1,2,3,4]
Output: "23:41"
Explanation: The valid 24-hour times are "12:34", "12:43", "13:24", "13:42", "14:23", "14:32", "21:34", "21:43", "23:14", and "23:41". Of these times, "23:41" is the latest.
```

```
Input: arr = [5,5,5,5]
Output: ""
Explanation: There are no valid 24-hour times as "55:55" is not valid.
```

### Constraints

* `arr.length == 4`
* `0 <= arr[i] <= 9`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_time_for_given_digits/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/largest_time_for_given_digits/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from itertools import permutations


class Solution:
    # Time: O(4! * 4)
    # Space: O(1)
    def largest_time_from_digits(self, arr: list[int]) -> str:
        best = ""
        for h1, h2, m1, m2 in permutations(arr):
            hour = h1 * 10 + h2
            minute = m1 * 10 + m2
            if hour < 24 and minute < 60:
                candidate = f"{hour:02d}:{minute:02d}"
                if candidate > best:
                    best = candidate
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(4! \* 4) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.