A vintage diner jukebox logs every digit key a customer presses on its song-selector dial over the course of a night, one after another, into a single digit string s. The night manager wants to find a self-tallying pair: the leftmost position i such that the digits at positions i and i+1 are different from each other, and each of those two digits' own face value equals exactly how many times that digit occurs anywhere in the whole log s (not just near the pair itself). If such a pair exists, report the two digits in the order they appear; otherwise report that none exists.
A single line containing the digit string s.
Print the two-character string formed by the leftmost self-tallying pair (the digit at position i followed by the digit at position i+1), or print -1 if no self-tallying pair exists anywhere in s.
Example 1
Input
2523533
Expected
23
Explanation
Counting occurrences across the whole log: '2' appears twice, '5' appears twice, '3' appears three times. Scanning adjacent pairs left to right: positions (0,1)='2','5' fail because '5' needs a count of 5 but only occurs twice; positions (1,2)='5','2' fail the same way; positions (2,3)='2','3' succeed because '2' occurs exactly 2 times, '3' occurs exactly 3 times, and the two digits differ. The leftmost self-tallying pair is "23".
Example 2
Input
1234
Expected
-1
Explanation
Every digit here occurs exactly once, so a digit would need to equal 1 to satisfy the count-matches-value rule. Only '1' occurs once and equals 1, but its neighbor '2' would need to occur twice while it only occurs once, so no adjacent pair qualifies anywhere in the string. The answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →