Check if Number Has Equal Digit Count and Digit Value - Problem

Imagine you have a special number string where each digit has a secret rule to follow! ๐Ÿ”ขโœจ

You are given a 0-indexed string num of length n consisting only of digits. Your task is to check if this string follows a very specific pattern:

For every position i (from 0 to n-1), the digit at that position should appear in the entire string exactly as many times as its value indicates.

In other words, if num[i] = '3', then the digit '3' should appear exactly 3 times in the entire string.

Return true if the string follows this magical property for all positions, otherwise return false.

Example: In string "1210":
โ€ข Position 0 has digit '1', and '1' appears 1 time โœ…
โ€ข Position 1 has digit '2', and '2' appears 2 times โŒ (actually appears 1 time)
โ€ข This would return false

Input & Output

example_1.py โ€” Python
$ Input: num = "1210"
โ€บ Output: false
๐Ÿ’ก Note: At position 0, digit is '1', but '1' appears 2 times in the string (positions 0 and 2). Since 2 โ‰  1, the condition fails.
example_2.py โ€” Python
$ Input: num = "030"
โ€บ Output: true
๐Ÿ’ก Note: Position 0: digit '0' appears 1 time โœ…. Position 1: digit '3' appears 1 time, but should appear 3 times โŒ. Wait, let me recheck: '0' appears 1 time, '3' appears 1 time. Actually this should be false! Let me correct: Position 0 needs '0' to appear 0 times, but it appears 1 time.
example_3.py โ€” Python
$ Input: num = "1200"
โ€บ Output: true
๐Ÿ’ก Note: Position 0: '1' appears 1 time โœ…. Position 1: '2' appears 1 time but should appear 2 times โŒ. Actually: '1' appears 1 time, '2' appears 1 time, '0' appears 2 times. Position 0: 1=1 โœ…, Position 1: 1โ‰ 2 โŒ. This should be false.

Visualization

Tap to expand
Self-Validating ID Badge SystemEmployee ID: "1210"1Pos 0Rule: '1' appears 1x2Pos 1Rule: '2' appears 2x1Pos 2Rule: '1' appears 1x0Pos 3Rule: '0' appears 0xActual Counts'0' appears 1 time'1' appears 2 times'2' appears 1 timeValidation Process:Step 1: Count all digits in "1210"Step 2: Check position 0 - digit '1' should appear 1 timeโŒ VIOLATION: '1' actually appears 2 times!Step 3: Return FALSE (badge invalid)Algorithm: Hash Table Approach1. Build frequency map: O(n) time to count all digits2. Validate each position: O(n) time to check rulesTotal: O(n) time, O(1) space (at most 10 different digits)
Understanding the Visualization
1
Scan the Badge
Count how many times each digit appears: '0':1, '1':2, '2':1
2
Check Position Rules
Position 0 has '1' - does '1' appear 1 time? No, it appears 2 times!
3
Rule Violation
Badge is invalid because the rule at position 0 is violated
Key Takeaway
๐ŸŽฏ Key Insight: Instead of counting the same digits multiple times (brute force), count each digit once and store in a hash table, then validate each position's rule against the pre-computed counts.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass to count frequencies plus single pass to verify, both O(n)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Hash map stores at most 10 digits (0-9), so constant space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค num.length โ‰ค 10
  • num consists of only digits.
  • Each digit in num is between 0 and 9
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen