-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dariel Rivero LLerena
authored and
Dariel Rivero LLerena
committed
Oct 16, 2022
1 parent
17f6cff
commit 0882cc7
Showing
5 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
.../java/drll/problems/leetcode/NumberOfValidClockTimes/NumberOfValidClockTimes.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
## Link to the problem | ||
|
||
- https://leetcode.com/problems/number-of-valid-clock-times/ | ||
|
||
##### Difficulty Level (according to Leetcode) | ||
|
||
> Easy ( String ) | ||
|
||
##### Resume of the problem: | ||
|
||
|
||
##### Notes | ||
|
40 changes: 40 additions & 0 deletions
40
src/main/java/drll/problems/leetcode/NumberOfValidClockTimes/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package drll.problems.leetcode.NumberOfValidClockTimes; | ||
|
||
public class Solution { | ||
public int countTime(String time) { | ||
String[] splitTime = time.split(":"); | ||
String hours = splitTime[0]; | ||
String minutes = splitTime[1]; | ||
|
||
int validHoursTimes = 1; | ||
int validMinutesTimes = 1; | ||
|
||
if(hours.charAt(0) == '?'){ | ||
if(hours.charAt(1) == '?'){ | ||
validHoursTimes = 3; | ||
} | ||
else{ | ||
int hour = Integer.parseInt(String.valueOf(hours.charAt(1))); | ||
validHoursTimes = hour < 4 ? 3 : 2; | ||
} | ||
} | ||
if(hours.charAt(1) == '?'){ | ||
if(hours.charAt(0) == '?'){ | ||
validHoursTimes = 24; | ||
} | ||
else{ | ||
validHoursTimes = hours.charAt(0) == '0' || hours.charAt(0) == '1' ? 10 : 4; | ||
} | ||
} | ||
|
||
|
||
if(minutes.charAt(0) == '?'){ | ||
validMinutesTimes = 6; | ||
} | ||
if(minutes.charAt(1) == '?'){ | ||
validMinutesTimes = minutes.charAt(0) == '?' ? 60 : 10; | ||
} | ||
|
||
return validHoursTimes * validMinutesTimes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/test/java/drll/problems/leetcode/NumberOfValidClockTimes/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package drll.problems.leetcode.NumberOfValidClockTimes; | ||
|
||
import drll.problems.leetcode.NumberOfValidClockTimes.Solution; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class SolutionTest { | ||
private Solution solution; | ||
|
||
@BeforeEach | ||
void init(){ | ||
solution = new Solution(); | ||
} | ||
|
||
@Test | ||
void should_return_correctly_for_test_case1() { | ||
assertThat(solution.countTime("?5:00")).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void should_return_correctly_for_test_case2() { | ||
assertThat(solution.countTime("0?:0?")).isEqualTo(100); | ||
} | ||
|
||
@Test | ||
void should_return_correctly_for_test_case3() { | ||
assertThat(solution.countTime("??:??")).isEqualTo(1440); | ||
} | ||
|
||
@Test | ||
void should_return_correctly_for_test_case4() { | ||
assertThat(solution.countTime("07:?3")).isEqualTo(6); | ||
} | ||
} |