Skip to content

Commit c9ec8a8

Browse files
solves #2283: Check if Number Has Equal Digit Count and Digit Value in java
1 parent f19f981 commit c9ec8a8

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@
737737
| 2269 | [Find the K-Beauty of a Number](https://leetcode.com/problems/find-the-k-beauty-of-a-number) | [![Java](assets/java.png)](src/FindTheKBeautyOfANumber.java) | |
738738
| 2273 | [Find Resultant Array After Removing Anagrams](https://leetcode.com/problems/find-resultant-array-after-removing-anagrams) | [![Java](assets/java.png)](src/FindResultantArrayAfterRemovingAnagrams.java) | |
739739
| 2278 | [Percentage of Letter in String](https://leetcode.com/problems/percentage-of-letter-in-string) | [![Java](assets/java.png)](src/PercentageOfLetterInString.java) | |
740-
| 2283 | [Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value) | | |
740+
| 2283 | [Check if Number Has Equal Digit Count and Digit Value](https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value) | [![Java](assets/java.png)](src/CheckIfNumberHasEqualDigitCountAndDigitValue.java) | |
741741
| 2287 | [Rearrange Characters to Make Target String](https://leetcode.com/problems/rearrange-characters-to-make-target-string) | | |
742742
| 2293 | [Min Max Game](https://leetcode.com/problems/min-max-game) | | |
743743
| 2299 | [Strong Password Checker II](https://leetcode.com/problems/strong-password-checker-ii) | | |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value
2+
// T: O(|num|)
3+
// S: O(1)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class CheckIfNumberHasEqualDigitCountAndDigitValue {
9+
public boolean digitCount(String num) {
10+
final Map<Integer, Integer> digitFrequencies = getDigitFrequencies(num);
11+
for (int index = 0 ; index < num.length() ; index++) {
12+
if ((num.charAt(index) - '0') != digitFrequencies.getOrDefault(index, 0)) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
19+
private Map<Integer, Integer> getDigitFrequencies(String number) {
20+
final Map<Integer, Integer> frequencies = new HashMap<>();
21+
for (int i = 0 ; i < number.length() ; i++) {
22+
final int digit = number.charAt(i) - '0';
23+
frequencies.put(digit, frequencies.getOrDefault(digit, 0) + 1);
24+
}
25+
return frequencies;
26+
}
27+
}

0 commit comments

Comments
 (0)