You are given a string s consisting only of digits. A valid pair is defined as two adjacent digits in s such that:
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:
Input: s = "2523533"
Output: "23"
Explanation:
Digit '2' appears 2 times and digit '3' appears 3 times. Each digit in the pair "23" appears in s exactly as many times as its numeric value. Hence, the output is "23".
Example 2:
Input: s = "221"
Output: "21"
Explanation:
Digit '2' appears 2 times and digit '1' appears 1 time. Hence, the output is "21".
Example 3:
Input: s = "22"
Output: ""
Explanation:
There are no valid adjacent pairs.
Constraints:
2 <= s.length <= 100s only consists of digits from '1' to '9'.Problem Overview: Given a string of digits, return the first adjacent pair where each digit appears in the entire string exactly as many times as its numeric value. If no such pair exists, return an empty string. The key requirement is verifying global digit frequencies while scanning adjacent characters.
Approach 1: Recount Frequencies for Every Pair (Brute Force) (Time: O(n^2), Space: O(1))
Check every adjacent pair s[i] and s[i+1]. For each pair, scan the entire string and count how many times both digits appear. If the frequency of each digit equals its numeric value and the digits are different, the pair is valid. This approach repeatedly recomputes counts, which leads to quadratic time. It works for very small inputs but scales poorly as the string length grows.
Approach 2: Frequency Counting + Single Pass Scan (Optimal) (Time: O(n), Space: O(1))
First compute digit frequencies using a small hash table or fixed array of size 10. Then iterate through the string once and inspect every adjacent pair. For digits a and b, check two conditions: they must be different, and their frequencies must match their numeric values (freq[a] == int(a) and freq[b] == int(b)). Because digit counts are precomputed, each validation becomes an O(1) lookup. The first pair that satisfies the rule is returned immediately.
This method relies on simple counting combined with constant‑time lookups using a small hash table. The string is scanned only once after counting, making it optimal for large inputs. Since digits are limited to 0–9, memory usage stays constant.
Recommended for interviews: The counting + single pass approach is the expected solution. It demonstrates that you separate global computation (digit frequencies) from local validation (adjacent pairs). Mentioning the brute force approach first shows you understand the naive strategy, while optimizing with precomputed counts and a linear scan shows strong problem‑solving skills with string processing.
We can use an array cnt of length 10 to record the occurrences of each digit in the string s.
Then, we traverse the adjacent digit pairs in the string s. If the two digits are not equal and the occurrences of these two digits are equal to the digits themselves, we have found a valid pair of adjacent digits and return it.
After traversing, if no valid pair of adjacent digits is found, we return an empty string.
The time complexity is O(n), where n is the length of the string s. The space complexity is O(|\Sigma|), where \Sigma is the character set of the string s. In this problem, \Sigma = {1, 2, ldots, 9}.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recount Frequencies for Each Pair (Brute Force) | O(n^2) | O(1) | Useful for explaining the baseline logic during interviews or when input size is very small. |
| Frequency Counting + Single Scan | O(n) | O(1) | Best general solution. Precompute counts once and validate adjacent pairs with constant‑time lookups. |
100552. Find Valid Pair of Adjacent Digits in String | Strings | Biweekly Contest 149 | Leetcode • Rapid Syntax • 213 views views
Watch 9 more video solutions →Practice Find Valid Pair of Adjacent Digits in String with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor