https://leetcode.com/problems/largest-time-for-given-digits/
public static String largestTimeFromDigits(int[] A) {
Time largestTime = null;
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
for (int k = 0; k < A.length; k++) {
for (int l = 0; l < A.length; l++) {
if (i == j || j == k || k == l || i == k || j == l || i == l) continue;
StringBuilder timeStringBuilder = new StringBuilder();
timeStringBuilder.append(A[i]).append(A[j]).append(":").append(A[k]).append(A[l]).append(":00");
try {
String[] split = timeStringBuilder.toString().split(":");
if (Integer.valueOf(split[0]) > 23 || Integer.valueOf(split[1]) > 59) continue;
Time time = Time.valueOf(timeStringBuilder.toString());
if (largestTime == null) largestTime = time;
int comparison = largestTime.compareTo(time);
if (comparison == -1) {
largestTime = time;
}
} catch (Exception e) { }
}
}
}
}
return largestTime == null ? "":largestTime.toString().substring(0, 5);
}
Given an array of 4 digits, return the largest 24 hour time that can be made.
The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight.
Return the answer as a string of length 5. If no valid time can be made, return an empty string.
Example 1:
Input: [1,2,3,4]
Output: "23:41"
Example 2:
Input: [5,5,5,5]
Output: ""