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
Visualization
Time & Space Complexity
Single pass to count frequencies plus single pass to verify, both O(n)
Hash map stores at most 10 digits (0-9), so constant space
Constraints
- 1 โค num.length โค 10
- num consists of only digits.
- Each digit in num is between 0 and 9