Find Valid Pair of Adjacent Digits in String

You are given a string s consisting only of digits. A valid pair is defined as two adjacent digits in s such that:

  • The first digit is not equal to the second.
  • Each digit in the pair appears in s exactly as many times as its numeric value.

Return the first valid pair found in the string s when traversing from left to right. If no valid pair exists, return an empty string.

Example 1
Inputs = "2523533"
Output"23"
Digit '2' appears 2 times and digit '3' appears 3 times, so the first valid pair is "23".
Example 2
Inputs = "221"
Output"21"
Digit '2' appears 2 times and digit '1' appears 1 time, so the output is "21".

Constraints

  • 2 <= s.length <= 100
  • s only consists of digits from '1' to '9'.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate