Skip to content

Commit d1eddfa

Browse files
authored
feat: add biweekly contest 149 (#4014)
1 parent bd1d988 commit d1eddfa

File tree

23 files changed

+1252
-1
lines changed

23 files changed

+1252
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3438.Find%20Valid%20Pair%20of%20Adjacent%20Digits%20in%20String/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3438. 找到字符串中合法的相邻数字](https://leetcode.cn/problems/find-valid-pair-of-adjacent-digits-in-string)
10+
11+
[English Version](/solution/3400-3499/3438.Find%20Valid%20Pair%20of%20Adjacent%20Digits%20in%20String/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个只包含数字的字符串&nbsp;<code>s</code>&nbsp;。如果 <code>s</code>&nbsp;中两个 <strong>相邻</strong>&nbsp;的数字满足以下条件,我们称它们是 <strong>合法的</strong>&nbsp;:</p>
18+
19+
<ul>
20+
<li>前面的数字 <strong>不等于</strong> 第二个数字。</li>
21+
<li>两个数字在 <code>s</code>&nbsp;中出现的次数 <strong>恰好</strong>&nbsp;分别等于这个数字本身。</li>
22+
</ul>
23+
24+
<p>请你从左到右遍历字符串 <code>s</code>&nbsp;,并返回最先找到的 <strong>合法</strong>&nbsp;相邻数字。如果这样的相邻数字不存在,请你返回一个空字符串。</p>
25+
26+
<p>&nbsp;</p>
27+
28+
<p><strong class="example">示例 1:</strong></p>
29+
30+
<div class="example-block">
31+
<p><span class="example-io"><b>输入:</b>s = "2523533"</span></p>
32+
33+
<p><span class="example-io"><b>输出:</b>"23"</span></p>
34+
35+
<p><strong>解释:</strong></p>
36+
37+
<p>数字&nbsp;<code>'2'</code>&nbsp;出现 2 次,数字&nbsp;<code>'3'</code>&nbsp;出现 3 次。<code>"23"</code>&nbsp;中每个数字在 <code>s</code>&nbsp;中出现的次数都恰好分别等于数字本身。所以输出&nbsp;<code>"23"</code>&nbsp;。</p>
38+
</div>
39+
40+
<p><strong class="example">示例 2:</strong></p>
41+
42+
<div class="example-block">
43+
<p><span class="example-io"><b>输入:</b>s = "221"</span></p>
44+
45+
<p><span class="example-io"><b>输出:</b>"21"</span></p>
46+
47+
<p><strong>解释:</strong></p>
48+
49+
<p>数字&nbsp;<code>'2'</code>&nbsp;出现 2 次,数字&nbsp;<code>'1'</code>&nbsp;出现 1 次。所以输出&nbsp;<code>"21"</code>&nbsp;。</p>
50+
</div>
51+
52+
<p><strong class="example">示例 3:</strong></p>
53+
54+
<div class="example-block">
55+
<p><span class="example-io"><b>输入:</b>s = "22"</span></p>
56+
57+
<p><span class="example-io"><b>输出:</b>""</span></p>
58+
59+
<p><strong>解释:</strong></p>
60+
61+
<p>没有合法的相邻数字。</p>
62+
</div>
63+
64+
<p>&nbsp;</p>
65+
66+
<p><strong>提示:</strong></p>
67+
68+
<ul>
69+
<li><code>2 &lt;= s.length &lt;= 100</code></li>
70+
<li><code>s</code>&nbsp;只包含&nbsp;<code>'1'</code> 到&nbsp;<code>'9'</code> 的数字。</li>
71+
</ul>
72+
73+
<!-- description:end -->
74+
75+
## 解法
76+
77+
<!-- solution:start -->
78+
79+
### 方法一:计数
80+
81+
我们可以用一个长度为 $10$ 的数组 $\textit{cnt}$ 记录字符串 $\textit{s}$ 中每个数字出现的次数。
82+
83+
然后我们遍历字符串 $\textit{s}$ 中的相邻数字对,如果这两个数字不相等且满足这两个数字出现的次数分别等于这两个数字本身,我们就找到了一个合法的相邻数字对,返回即可。
84+
85+
遍历结束后,如果没有找到合法的相邻数字对,我们返回一个空字符串。
86+
87+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{s}$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 为字符串 $\textit{s}$ 中出现的字符集,本题中 $\Sigma = \{1, 2, \ldots, 9\}$。
88+
89+
<!-- tabs:start -->
90+
91+
#### Python3
92+
93+
```python
94+
class Solution:
95+
def findValidPair(self, s: str) -> str:
96+
cnt = [0] * 10
97+
for x in map(int, s):
98+
cnt[x] += 1
99+
for x, y in pairwise(map(int, s)):
100+
if x != y and cnt[x] == x and cnt[y] == y:
101+
return f"{x}{y}"
102+
return ""
103+
```
104+
105+
#### Java
106+
107+
```java
108+
class Solution {
109+
public String findValidPair(String s) {
110+
int[] cnt = new int[10];
111+
for (char c : s.toCharArray()) {
112+
++cnt[c - '0'];
113+
}
114+
for (int i = 1; i < s.length(); ++i) {
115+
int x = s.charAt(i - 1) - '0';
116+
int y = s.charAt(i) - '0';
117+
if (x != y && cnt[x] == x && cnt[y] == y) {
118+
return s.substring(i - 1, i + 1);
119+
}
120+
}
121+
return "";
122+
}
123+
}
124+
```
125+
126+
#### C++
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
string findValidPair(string s) {
132+
int cnt[10]{};
133+
for (char c : s) {
134+
++cnt[c - '0'];
135+
}
136+
for (int i = 1; i < s.size(); ++i) {
137+
int x = s[i - 1] - '0';
138+
int y = s[i] - '0';
139+
if (x != y && cnt[x] == x && cnt[y] == y) {
140+
return s.substr(i - 1, 2);
141+
}
142+
}
143+
return "";
144+
}
145+
};
146+
```
147+
148+
#### Go
149+
150+
```go
151+
func findValidPair(s string) string {
152+
cnt := [10]int{}
153+
for _, c := range s {
154+
cnt[c-'0']++
155+
}
156+
for i := 1; i < len(s); i++ {
157+
x, y := int(s[i-1]-'0'), int(s[i]-'0')
158+
if x != y && cnt[x] == x && cnt[y] == y {
159+
return s[i-1 : i+1]
160+
}
161+
}
162+
return ""
163+
}
164+
```
165+
166+
#### TypeScript
167+
168+
```ts
169+
function findValidPair(s: string): string {
170+
const cnt: number[] = Array(10).fill(0);
171+
for (const c of s) {
172+
++cnt[+c];
173+
}
174+
for (let i = 1; i < s.length; ++i) {
175+
const x = +s[i - 1];
176+
const y = +s[i];
177+
if (x !== y && cnt[x] === x && cnt[y] === y) {
178+
return `${x}${y}`;
179+
}
180+
}
181+
return '';
182+
}
183+
```
184+
185+
<!-- tabs:end -->
186+
187+
<!-- solution:end -->
188+
189+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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>&nbsp;</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 = &quot;2523533&quot;</span></p>
31+
32+
<p><strong>Output:</strong> <span class="example-io">&quot;23&quot;</span></p>
33+
34+
<p><strong>Explanation:</strong></p>
35+
36+
<p>Digit <code>&#39;2&#39;</code> appears 2 times and digit <code>&#39;3&#39;</code> appears 3 times. Each digit in the pair <code>&quot;23&quot;</code> appears in <code>s</code> exactly as many times as its numeric value. Hence, the output is <code>&quot;23&quot;</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 = &quot;221&quot;</span></p>
43+
44+
<p><strong>Output:</strong> <span class="example-io">&quot;21&quot;</span></p>
45+
46+
<p><strong>Explanation:</strong></p>
47+
48+
<p>Digit <code>&#39;2&#39;</code> appears 2 times and digit <code>&#39;1&#39;</code> appears 1 time. Hence, the output is <code>&quot;21&quot;</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 = &quot;22&quot;</span></p>
55+
56+
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
57+
58+
<p><strong>Explanation:</strong></p>
59+
60+
<p>There are no valid adjacent pairs.</p>
61+
</div>
62+
63+
<p>&nbsp;</p>
64+
<p><strong>Constraints:</strong></p>
65+
66+
<ul>
67+
<li><code>2 &lt;= s.length &lt;= 100</code></li>
68+
<li><code>s</code> only consists of digits from <code>&#39;1&#39;</code> to <code>&#39;9&#39;</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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
string findValidPair(string s) {
4+
int cnt[10]{};
5+
for (char c : s) {
6+
++cnt[c - '0'];
7+
}
8+
for (int i = 1; i < s.size(); ++i) {
9+
int x = s[i - 1] - '0';
10+
int y = s[i] - '0';
11+
if (x != y && cnt[x] == x && cnt[y] == y) {
12+
return s.substr(i - 1, 2);
13+
}
14+
}
15+
return "";
16+
}
17+
};

0 commit comments

Comments
 (0)