Find Valid Pair of Adjacent Digits in String - Problem
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 digit
- Each digit in the pair appears in
sexactly 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.
Input & Output
Example 1 — Basic Valid Pair
$
Input:
s = "12332"
›
Output:
"12"
💡 Note:
Adjacent digits '1' and '2': digit 1 appears 1 time (matches its value 1), digit 2 appears 2 times (matches its value 2). Both conditions satisfied, so return "12".
Example 2 — No Valid Pair
$
Input:
s = "1234"
›
Output:
""
💡 Note:
Check each adjacent pair: '1' appears 1 time = 1 ✓, but '2' appears 1 time ≠ 2 ✗. Continue: '2' appears 1 time ≠ 2 ✗. No pair satisfies both conditions.
Example 3 — Early Valid Pair
$
Input:
s = "1122"
›
Output:
""
💡 Note:
Check pairs: '1' appears 2 times ≠ 1, so no pair starting with '1' can be valid. '2' appears 2 times = 2, but needs to be paired with a valid digit. No valid pairs exist.
Constraints
- 1 ≤ s.length ≤ 1000
- s consists only of digits '0' to '9'
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code