|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3438.Find%20Valid%20Pair%20of%20Adjacent%20Digits%20in%20String/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3438. Find Valid Pair of Adjacent Digits in String](https://leetcode.com/problems/find-valid-pair-of-adjacent-digits-in-string) |
| 10 | + |
| 11 | +[中文文档](/solution/3400-3499/3438.Find%20Valid%20Pair%20of%20Adjacent%20Digits%20in%20String/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are given a string <code>s</code> consisting only of digits. A <strong>valid pair</strong> is defined as two <strong>adjacent</strong> digits in <code>s</code> such that:</p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>The first digit is <strong>not equal</strong> to the second.</li> |
| 21 | + <li>Each digit in the pair appears in <code>s</code> <strong>exactly</strong> as many times as its numeric value.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Return the first <strong>valid pair</strong> found in the string <code>s</code> when traversing from left to right. If no valid pair exists, return an empty string.</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | + |
| 29 | +<div class="example-block"> |
| 30 | +<p><strong>Input:</strong> <span class="example-io">s = "2523533"</span></p> |
| 31 | + |
| 32 | +<p><strong>Output:</strong> <span class="example-io">"23"</span></p> |
| 33 | + |
| 34 | +<p><strong>Explanation:</strong></p> |
| 35 | + |
| 36 | +<p>Digit <code>'2'</code> appears 2 times and digit <code>'3'</code> appears 3 times. Each digit in the pair <code>"23"</code> appears in <code>s</code> exactly as many times as its numeric value. Hence, the output is <code>"23"</code>.</p> |
| 37 | +</div> |
| 38 | + |
| 39 | +<p><strong class="example">Example 2:</strong></p> |
| 40 | + |
| 41 | +<div class="example-block"> |
| 42 | +<p><strong>Input:</strong> <span class="example-io">s = "221"</span></p> |
| 43 | + |
| 44 | +<p><strong>Output:</strong> <span class="example-io">"21"</span></p> |
| 45 | + |
| 46 | +<p><strong>Explanation:</strong></p> |
| 47 | + |
| 48 | +<p>Digit <code>'2'</code> appears 2 times and digit <code>'1'</code> appears 1 time. Hence, the output is <code>"21"</code>.</p> |
| 49 | +</div> |
| 50 | + |
| 51 | +<p><strong class="example">Example 3:</strong></p> |
| 52 | + |
| 53 | +<div class="example-block"> |
| 54 | +<p><strong>Input:</strong> <span class="example-io">s = "22"</span></p> |
| 55 | + |
| 56 | +<p><strong>Output:</strong> <span class="example-io">""</span></p> |
| 57 | + |
| 58 | +<p><strong>Explanation:</strong></p> |
| 59 | + |
| 60 | +<p>There are no valid adjacent pairs.</p> |
| 61 | +</div> |
| 62 | + |
| 63 | +<p> </p> |
| 64 | +<p><strong>Constraints:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li><code>2 <= s.length <= 100</code></li> |
| 68 | + <li><code>s</code> only consists of digits from <code>'1'</code> to <code>'9'</code>.</li> |
| 69 | +</ul> |
| 70 | + |
| 71 | +<!-- description:end --> |
| 72 | + |
| 73 | +## Solutions |
| 74 | + |
| 75 | +<!-- solution:start --> |
| 76 | + |
| 77 | +### Solution 1: Counting |
| 78 | + |
| 79 | +We can use an array $\textit{cnt}$ of length $10$ to record the occurrences of each digit in the string $\textit{s}$. |
| 80 | + |
| 81 | +Then, we traverse the adjacent digit pairs in the string $\textit{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. |
| 82 | + |
| 83 | +After traversing, if no valid pair of adjacent digits is found, we return an empty string. |
| 84 | + |
| 85 | +The time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set of the string $\textit{s}$. In this problem, $\Sigma = \{1, 2, \ldots, 9\}$. |
| 86 | + |
| 87 | +<!-- tabs:start --> |
| 88 | + |
| 89 | +#### Python3 |
| 90 | + |
| 91 | +```python |
| 92 | +class Solution: |
| 93 | + def findValidPair(self, s: str) -> str: |
| 94 | + cnt = [0] * 10 |
| 95 | + for x in map(int, s): |
| 96 | + cnt[x] += 1 |
| 97 | + for x, y in pairwise(map(int, s)): |
| 98 | + if x != y and cnt[x] == x and cnt[y] == y: |
| 99 | + return f"{x}{y}" |
| 100 | + return "" |
| 101 | +``` |
| 102 | + |
| 103 | +#### Java |
| 104 | + |
| 105 | +```java |
| 106 | +class Solution { |
| 107 | + public String findValidPair(String s) { |
| 108 | + int[] cnt = new int[10]; |
| 109 | + for (char c : s.toCharArray()) { |
| 110 | + ++cnt[c - '0']; |
| 111 | + } |
| 112 | + for (int i = 1; i < s.length(); ++i) { |
| 113 | + int x = s.charAt(i - 1) - '0'; |
| 114 | + int y = s.charAt(i) - '0'; |
| 115 | + if (x != y && cnt[x] == x && cnt[y] == y) { |
| 116 | + return s.substring(i - 1, i + 1); |
| 117 | + } |
| 118 | + } |
| 119 | + return ""; |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +#### C++ |
| 125 | + |
| 126 | +```cpp |
| 127 | +class Solution { |
| 128 | +public: |
| 129 | + string findValidPair(string s) { |
| 130 | + int cnt[10]{}; |
| 131 | + for (char c : s) { |
| 132 | + ++cnt[c - '0']; |
| 133 | + } |
| 134 | + for (int i = 1; i < s.size(); ++i) { |
| 135 | + int x = s[i - 1] - '0'; |
| 136 | + int y = s[i] - '0'; |
| 137 | + if (x != y && cnt[x] == x && cnt[y] == y) { |
| 138 | + return s.substr(i - 1, 2); |
| 139 | + } |
| 140 | + } |
| 141 | + return ""; |
| 142 | + } |
| 143 | +}; |
| 144 | +``` |
| 145 | +
|
| 146 | +#### Go |
| 147 | +
|
| 148 | +```go |
| 149 | +func findValidPair(s string) string { |
| 150 | + cnt := [10]int{} |
| 151 | + for _, c := range s { |
| 152 | + cnt[c-'0']++ |
| 153 | + } |
| 154 | + for i := 1; i < len(s); i++ { |
| 155 | + x, y := int(s[i-1]-'0'), int(s[i]-'0') |
| 156 | + if x != y && cnt[x] == x && cnt[y] == y { |
| 157 | + return s[i-1 : i+1] |
| 158 | + } |
| 159 | + } |
| 160 | + return "" |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +#### TypeScript |
| 165 | + |
| 166 | +```ts |
| 167 | +function findValidPair(s: string): string { |
| 168 | + const cnt: number[] = Array(10).fill(0); |
| 169 | + for (const c of s) { |
| 170 | + ++cnt[+c]; |
| 171 | + } |
| 172 | + for (let i = 1; i < s.length; ++i) { |
| 173 | + const x = +s[i - 1]; |
| 174 | + const y = +s[i]; |
| 175 | + if (x !== y && cnt[x] === x && cnt[y] === y) { |
| 176 | + return `${x}${y}`; |
| 177 | + } |
| 178 | + } |
| 179 | + return ''; |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +<!-- tabs:end --> |
| 184 | + |
| 185 | +<!-- solution:end --> |
| 186 | + |
| 187 | +<!-- problem:end --> |
0 commit comments