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:
"23"
💡 Note:
Adjacent digits '2' and '3': digit 2 appears 2 times (matches its value), digit 3 appears 2 times (doesn't match its value 3). Continue checking. Next pair '3' and '3' are equal, skip. Pair '3' and '2': 3 appears 2 times ≠ 3, invalid. But wait - let me recheck: in "12332", pair "23" at positions 1,2 has 2 appearing 2 times = 2 ✓, and 3 appearing 2 times ≠ 3 ✗. Actually, looking for the first valid pair correctly.
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:
"12"
💡 Note:
First pair '1','2': digit 1 appears 2 times ≠ 1 ✗. Second pair '1','2': same issue. Third pair '2','2': digits are equal, skip. Need to find where both digits have correct frequencies.
Constraints
- 1 ≤ s.length ≤ 1000
- s consists only of digits '0' to '9'
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
String with digit frequency requirements
2
Frequency Check
Each digit must appear exactly as many times as its value
3
Pair Validation
Find first adjacent pair where both satisfy frequency rule
Key Takeaway
🎯 Key Insight: Pre-compute digit frequencies, then scan adjacent pairs for simultaneous validation
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code