Skip to content

Commit bb42bc1

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18 MB (100.00%)
1 parent 09de65f commit bb42bc1

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<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>
2+
3+
<ul>
4+
<li>The first digit is <strong>not equal</strong> to the second.</li>
5+
<li>Each digit in the pair appears in <code>s</code> <strong>exactly</strong> as many times as its numeric value.</li>
6+
</ul>
7+
8+
<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>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<div class="example-block">
14+
<p><strong>Input:</strong> <span class="example-io">s = &quot;2523533&quot;</span></p>
15+
16+
<p><strong>Output:</strong> <span class="example-io">&quot;23&quot;</span></p>
17+
18+
<p><strong>Explanation:</strong></p>
19+
20+
<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>
21+
</div>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<div class="example-block">
26+
<p><strong>Input:</strong> <span class="example-io">s = &quot;221&quot;</span></p>
27+
28+
<p><strong>Output:</strong> <span class="example-io">&quot;21&quot;</span></p>
29+
30+
<p><strong>Explanation:</strong></p>
31+
32+
<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>
33+
</div>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>Input:</strong> <span class="example-io">s = &quot;22&quot;</span></p>
39+
40+
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
41+
42+
<p><strong>Explanation:</strong></p>
43+
44+
<p>There are no valid adjacent pairs.</p>
45+
</div>
46+
47+
<p>&nbsp;</p>
48+
<p><strong>Constraints:</strong></p>
49+
50+
<ul>
51+
<li><code>2 &lt;= s.length &lt;= 100</code></li>
52+
<li><code>s</code> only consists of digits from <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code>.</li>
53+
</ul>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
from collections import Counter
5+
6+
class Solution:
7+
def findValidPair(self, s: str) -> str:
8+
freq = Counter(s)
9+
10+
for i in range(len(s) - 1):
11+
first = s[i]
12+
second = s[i + 1]
13+
14+
if (first != second and
15+
freq[first] == int(first) and
16+
freq[second] == int(second)):
17+
18+
return first + second
19+
20+
return ''
21+

0 commit comments

Comments
 (0)