Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added task 2283
  • Loading branch information
AryamanMishra committed Jun 17, 2022
commit 5e594182d6d9c6c8a037b9cdc07d334d4af77f23
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g2201_2300.s2283_check_if_number_has_equal_digit_count_and_digit_value;

// #Easy #Hash_Table #String #Counting #2022_06_17_Time_1_ms_(99.36%)_Space_40.4_MB_(96.46%)

public class Solution {
public boolean digitCount(String num) {
int[] cnt = new int[11];
char[] arr = num.toCharArray();
for (char d : arr) {
++cnt[d - '0'];
}
for (int i = 0; i < arr.length; i++) {
if (cnt[i] != arr[i] - '0') {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
2283\. Check if Number Has Equal Digit Count and Digit Value

Easy

You are given a **0-indexed** string `num` of length `n` consisting of digits.

Return `true` _if for **every** index_ `i` _in the range_ `0 <= i < n`_, the digit_ `i` _occurs_ `num[i]` _times in_ `num`_, otherwise return_ `false`.

**Example 1:**

**Input:** num = "1210"

**Output:** true

**Explanation:**

num[0] = '1'. The digit 0 occurs once in num.

num[1] = '2'. The digit 1 occurs twice in num.

num[2] = '1'. The digit 2 occurs once in num.

num[3] = '0'. The digit 3 occurs zero times in num.

The condition holds true for every index in "1210", so return true.

**Example 2:**

**Input:** num = "030"

**Output:** false

**Explanation:**

num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num.

num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num.

num[2] = '0'. The digit 2 occurs zero times in num.

The indices 0 and 1 both violate the condition, so return false.

**Constraints:**

* `n == num.length`
* `1 <= n <= 10`
* `num` consists of digits.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g2201_2300.s2283_check_if_number_has_equal_digit_count_and_digit_value;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void digitCount() {
assertThat(new Solution().digitCount("1210"), equalTo(true));
}

@Test
void digitCount2() {
assertThat(new Solution().digitCount("030"), equalTo(false));
}
}