Ctrl K

Self-Counting Code

Easy
The string code consists of digits. Call it **self-describing** if, for every position i (0-based), the digit code[i] equals how many times the digit i appears in the whole of code. For example, in "3211000": position 0 holds 3, and 0 appears three times; position 1 holds 2, and 1 appears twice; position 2 holds 1, and 2 appears once; and so on. Return True if code is self-describing, and False otherwise.
Example 1
code="2020" → True

There are two 0s, no 1s, two 2s and no 3s — exactly what positions 0, 1, 2, 3 say.

Example 2
code="21200" → True

Two 0s, one 1, two 2s, no 3s and no 4s — everything matches.

Example 3
code="11" → False

Position 0 says 1, but the code contains no 0 at all.

Constraints
  • 1 ≤ len(code) ≤ 10
  • code consists of digits only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNote From Clipped Letters EasyFirst One-Off Letter EasyWords Heard Only Once EasyWords From Letter Tiles EasyBalloon Signs From Letters Easy
loading Python…
Input
code="2020"
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 140 tests on the server, hidden ones included.