Skip to content

Commit f194fb4

Browse files
solves #3114: Latest Time You Can Obtain After Replacing Characters in java
1 parent de065fa commit f194fb4

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@
904904
| 3099 | [Harshad Number](https://leetcode.com/problems/harshad-number) | [![Java](assets/java.png)](src/HarshadNumber.java) | |
905905
| 3105 | [Longest Strictly Increasing or Strictly Decreasing Subarray](https://leetcode.com/problems/longest-strictly-increasing-or-strictly-decreasing-subarray) | [![Java](assets/java.png)](src/LongestStrictlyIncreasingOrStrictlyDecreasingSubarray.java) | |
906906
| 3110 | [Score of a String](https://leetcode.com/problems/score-of-a-string) | [![Java](assets/java.png)](src/ScoreOfAString.java) | |
907-
| 3114 | [Latest Time You Can Obtain After Replacing Characters](https://leetcode.com/problems/latest-time-you-can-obtain-after-replacing-characters) | | |
907+
| 3114 | [Latest Time You Can Obtain After Replacing Characters](https://leetcode.com/problems/latest-time-you-can-obtain-after-replacing-characters) | [![Java](assets/java.png)](src/LatestTimeYouCanObtainAfterReplacingCharacters.java) | |
908908
| 3120 | [Count the Number of Special Characters I](https://leetcode.com/problems/count-the-number-of-special-characters-i) | | |
909909
| 3127 | [Make a Square with the Same Color](https://leetcode.com/problems/make-a-square-with-the-same-color) | | |
910910
| 3131 | [Find the Integer Added to Array I](https://leetcode.com/problems/find-the-integer-added-to-array-i) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// https://leetcode.com/problems/latest-time-you-can-obtain-after-replacing-characters
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class LatestTimeYouCanObtainAfterReplacingCharacters {
6+
public String findLatestTime(String s) {
7+
return maxHour(s.substring(0, 2)) + ":" + maxMinutes(s.substring(3));
8+
}
9+
10+
private static String maxHour(String s) {
11+
if (s.charAt(0) == '?' && s.charAt(1) == '?') {
12+
return "11";
13+
}
14+
if (s.charAt(0) == '?') {
15+
return (s.charAt(1) <= '1' ? "1" : "0") + s.charAt(1);
16+
}
17+
if (s.charAt(1) == '?') {
18+
return s.charAt(0) + (s.charAt(0) == '0' ? "9" : "1");
19+
}
20+
return s;
21+
}
22+
23+
private static String maxMinutes(String s) {
24+
if (s.charAt(0) == '?' && s.charAt(1) == '?') {
25+
return "59";
26+
}
27+
if (s.charAt(0) == '?') {
28+
return "5" + s.charAt(1);
29+
}
30+
if (s.charAt(1) == '?') {
31+
return s.charAt(0) + "9";
32+
}
33+
return s;
34+
}
35+
}

0 commit comments

Comments
 (0)