Ctrl K

Self-Counting Digit Pair

Easy
You are given a string digits made of the digits 1 through 9. Two **adjacent** digits form a matching pair when: - the two digits are different; - each of them occurs in the whole string exactly as many times as its own value (for example, 3 occurs exactly three times). Scanning from left to right, find the first matching pair and return it as a two-character string. If there is no such pair, return an empty string.
Example 1
digits='4134344' → '41'

The pair "41": 4 occurs four times, 1 occurs once, and the digits differ. It is the leftmost such pair.

Example 2
digits='3313' → '31'

3 occurs three times and 1 once. "33" does not count since the digits are equal, so the first fit is "31".

Example 3
digits='5512' → ''

1 occurs once, but its neighbors fail: 5 occurs twice and 2 only once. No pair qualifies, so the answer is an empty string.

Constraints
  • 2 ≤ digits.length ≤ 100
  • digits consists only of the digits 1 to 9
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
digits='4134344'
Expected
'41'

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