Skip to content

Commit 3c2e771

Browse files
authored
feat: add weekly contest 385 (doocs#2349)
1 parent 6aad117 commit 3c2e771

File tree

13 files changed

+704
-0
lines changed

13 files changed

+704
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [3042. 统计前后缀下标对 I](https://leetcode.cn/problems/count-prefix-and-suffix-pairs-i)
2+
3+
[English Version](/solution/3000-3099/3042.Count%20Prefix%20and%20Suffix%20Pairs%20I/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 。</p>
10+
11+
<p>定义一个 <strong>布尔 </strong>函数 <code>isPrefixAndSuffix</code> ,它接受两个字符串参数 <code>str1</code> 和 <code>str2</code> :</p>
12+
13+
<ul>
14+
<li>当 <code>str1</code> 同时是 <code>str2</code> 的前缀(<span data-keyword="string-prefix">prefix</span>)和后缀(<span data-keyword="string-suffix">suffix</span>)时,<code>isPrefixAndSuffix(str1, str2)</code> 返回 <code>true</code>,否则返回 <code>false</code>。</li>
15+
</ul>
16+
17+
<p>例如,<code>isPrefixAndSuffix("aba", "ababa")</code> 返回 <code>true</code>,因为 <code>"aba"</code> 既是 <code>"ababa"</code> 的前缀,也是 <code>"ababa"</code> 的后缀,但是 <code>isPrefixAndSuffix("abc", "abcd")</code> 返回<code> false</code>。</p>
18+
19+
<p>以整数形式,返回满足 <code>i &lt; j</code> 且 <code>isPrefixAndSuffix(words[i], words[j])</code> 为 <code>true</code> 的下标对 <code>(i, j)</code> 的<strong> 数量 </strong>。</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong class="example">示例 1:</strong></p>
24+
25+
<pre>
26+
<strong>输入:</strong>words = ["a","aba","ababa","aa"]
27+
<strong>输出:</strong>4
28+
<strong>解释:</strong>在本示例中,计数的下标对包括:
29+
i = 0 且 j = 1 ,因为 isPrefixAndSuffix("a", "aba") 为 true 。
30+
i = 0 且 j = 2 ,因为 isPrefixAndSuffix("a", "ababa") 为 true 。
31+
i = 0 且 j = 3 ,因为 isPrefixAndSuffix("a", "aa") 为 true 。
32+
i = 1 且 j = 2 ,因为 isPrefixAndSuffix("aba", "ababa") 为 true 。
33+
因此,答案是 4 。</pre>
34+
35+
<p><strong class="example">示例 2:</strong></p>
36+
37+
<pre>
38+
<strong>输入:</strong>words = ["pa","papa","ma","mama"]
39+
<strong>输出:</strong>2
40+
<strong>解释:</strong>在本示例中,计数的下标对包括:
41+
i = 0 且 j = 1 ,因为 isPrefixAndSuffix("pa", "papa") 为 true 。
42+
i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。
43+
因此,答案是 2 。</pre>
44+
45+
<p><strong class="example">示例 3:</strong></p>
46+
47+
<pre>
48+
<strong>输入:</strong>words = ["abab","ab"]
49+
<strong>输出:</strong>0
50+
<strong>解释:</strong>在本示例中,唯一有效的下标对是 i = 0 且 j = 1 ,但是 isPrefixAndSuffix("abab", "ab") 为 false 。
51+
因此,答案是 0 。</pre>
52+
53+
<p>&nbsp;</p>
54+
55+
<p><strong>提示:</strong></p>
56+
57+
<ul>
58+
<li><code>1 &lt;= words.length &lt;= 50</code></li>
59+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
60+
<li><code>words[i]</code> 仅由小写英文字母组成。</li>
61+
</ul>
62+
63+
## 解法
64+
65+
### 方法一
66+
67+
<!-- tabs:start -->
68+
69+
```python
70+
71+
```
72+
73+
```java
74+
75+
```
76+
77+
```cpp
78+
79+
```
80+
81+
```go
82+
83+
```
84+
85+
<!-- tabs:end -->
86+
87+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [3042. Count Prefix and Suffix Pairs I](https://leetcode.com/problems/count-prefix-and-suffix-pairs-i)
2+
3+
[中文文档](/solution/3000-3099/3042.Count%20Prefix%20and%20Suffix%20Pairs%20I/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
8+
9+
<p>Let&#39;s define a <strong>boolean</strong> function <code>isPrefixAndSuffix</code> that takes two strings, <code>str1</code> and <code>str2</code>:</p>
10+
11+
<ul>
12+
<li><code>isPrefixAndSuffix(str1, str2)</code> returns <code>true</code> if <code>str1</code> is <strong>both</strong> a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of <code>str2</code>, and <code>false</code> otherwise.</li>
13+
</ul>
14+
15+
<p>For example, <code>isPrefixAndSuffix(&quot;aba&quot;, &quot;ababa&quot;)</code> is <code>true</code> because <code>&quot;aba&quot;</code> is a prefix of <code>&quot;ababa&quot;</code> and also a suffix, but <code>isPrefixAndSuffix(&quot;abc&quot;, &quot;abcd&quot;)</code> is <code>false</code>.</p>
16+
17+
<p>Return <em>an integer denoting the <strong>number</strong> of index pairs </em><code>(i, j)</code><em> such that </em><code>i &lt; j</code><em>, and </em><code>isPrefixAndSuffix(words[i], words[j])</code><em> is </em><code>true</code><em>.</em></p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> words = [&quot;a&quot;,&quot;aba&quot;,&quot;ababa&quot;,&quot;aa&quot;]
24+
<strong>Output:</strong> 4
25+
<strong>Explanation:</strong> In this example, the counted index pairs are:
26+
i = 0 and j = 1 because isPrefixAndSuffix(&quot;a&quot;, &quot;aba&quot;) is true.
27+
i = 0 and j = 2 because isPrefixAndSuffix(&quot;a&quot;, &quot;ababa&quot;) is true.
28+
i = 0 and j = 3 because isPrefixAndSuffix(&quot;a&quot;, &quot;aa&quot;) is true.
29+
i = 1 and j = 2 because isPrefixAndSuffix(&quot;aba&quot;, &quot;ababa&quot;) is true.
30+
Therefore, the answer is 4.</pre>
31+
32+
<p><strong class="example">Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong> words = [&quot;pa&quot;,&quot;papa&quot;,&quot;ma&quot;,&quot;mama&quot;]
36+
<strong>Output:</strong> 2
37+
<strong>Explanation:</strong> In this example, the counted index pairs are:
38+
i = 0 and j = 1 because isPrefixAndSuffix(&quot;pa&quot;, &quot;papa&quot;) is true.
39+
i = 2 and j = 3 because isPrefixAndSuffix(&quot;ma&quot;, &quot;mama&quot;) is true.
40+
Therefore, the answer is 2. </pre>
41+
42+
<p><strong class="example">Example 3:</strong></p>
43+
44+
<pre>
45+
<strong>Input:</strong> words = [&quot;abab&quot;,&quot;ab&quot;]
46+
<strong>Output:</strong> 0
47+
<strong>Explanation: </strong>In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix(&quot;abab&quot;, &quot;ab&quot;) is false.
48+
Therefore, the answer is 0.</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= words.length &lt;= 50</code></li>
55+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
56+
<li><code>words[i]</code> consists only of lowercase English letters.</li>
57+
</ul>
58+
59+
## Solutions
60+
61+
### Solution 1
62+
63+
<!-- tabs:start -->
64+
65+
```python
66+
67+
```
68+
69+
```java
70+
71+
```
72+
73+
```cpp
74+
75+
```
76+
77+
```go
78+
79+
```
80+
81+
<!-- tabs:end -->
82+
83+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [3043. 最长公共前缀的长度](https://leetcode.cn/problems/find-the-length-of-the-longest-common-prefix)
2+
3+
[English Version](/solution/3000-3099/3043.Find%20the%20Length%20of%20the%20Longest%20Common%20Prefix/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你两个 <strong>正整数 </strong>数组 <code>arr1</code> 和 <code>arr2</code> 。</p>
10+
11+
<p>正整数的 <strong>前缀 </strong>是其 <strong>最左边 </strong>的一位或多位数字组成的整数。例如,<code>123</code> 是整数 <code>12345</code> 的前缀,而 <code>234</code><strong> 不是 </strong>。</p>
12+
13+
<p>设若整数 <code>c</code> 是整数 <code>a</code> 和 <code>b</code> 的<strong> 公共前缀 </strong>,那么 <code>c</code> 需要同时是 <code>a</code> 和 <code>b</code> 的前缀。例如,<code>5655359</code> 和 <code>56554</code> 有公共前缀 <code>565</code> ,而 <code>1223</code> 和 <code>43456</code><strong> 没有 </strong>公共前缀。</p>
14+
15+
<p>你需要找出属于 <code>arr1</code> 的整数 <code>x</code> 和属于 <code>arr2</code> 的整数 <code>y</code> 组成的所有数对 <code>(x, y)</code> 之中最长的公共前缀的长度。</p>
16+
17+
<p>返回所有数对之中最长公共前缀的长度。如果它们之间不存在公共前缀,则返回 <code>0</code> 。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong class="example">示例 1:</strong></p>
22+
23+
<pre>
24+
<strong>输入:</strong>arr1 = [1,10,100], arr2 = [1000]
25+
<strong>输出:</strong>3
26+
<strong>解释:</strong>存在 3 个数对 (arr1[i], arr2[j]) :
27+
- (1, 1000) 的最长公共前缀是 1 。
28+
- (10, 1000) 的最长公共前缀是 10 。
29+
- (100, 1000) 的最长公共前缀是 100 。
30+
最长的公共前缀是 100 ,长度为 3 。
31+
</pre>
32+
33+
<p><strong class="example">示例 2:</strong></p>
34+
35+
<pre>
36+
<strong>输入:</strong>arr1 = [1,2,3], arr2 = [4,4,4]
37+
<strong>输出:</strong>0
38+
<strong>解释:</strong>任何数对 (arr1[i], arr2[j]) 之中都不存在公共前缀,因此返回 0 。
39+
请注意,同一个数组内元素之间的公共前缀不在考虑范围内。
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= arr1.length, arr2.length &lt;= 5 * 10<sup>4</sup></code></li>
48+
<li><code>1 &lt;= arr1[i], arr2[i] &lt;= 10<sup>8</sup></code></li>
49+
</ul>
50+
51+
## 解法
52+
53+
### 方法一
54+
55+
<!-- tabs:start -->
56+
57+
```python
58+
59+
```
60+
61+
```java
62+
63+
```
64+
65+
```cpp
66+
67+
```
68+
69+
```go
70+
71+
```
72+
73+
<!-- tabs:end -->
74+
75+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [3043. Find the Length of the Longest Common Prefix](https://leetcode.com/problems/find-the-length-of-the-longest-common-prefix)
2+
3+
[中文文档](/solution/3000-3099/3043.Find%20the%20Length%20of%20the%20Longest%20Common%20Prefix/README.md)
4+
5+
## Description
6+
7+
<p>You are given two arrays with <strong>positive</strong> integers <code>arr1</code> and <code>arr2</code>.</p>
8+
9+
<p>A <strong>prefix</strong> of a positive integer is an integer formed by one or more of its digits, starting from its <strong>leftmost</strong> digit. For example, <code>123</code> is a prefix of the integer <code>12345</code>, while <code>234</code> is <strong>not</strong>.</p>
10+
11+
<p>A <strong>common prefix</strong> of two integers <code>a</code> and <code>b</code> is an integer <code>c</code>, such that <code>c</code> is a prefix of both <code>a</code> and <code>b</code>. For example, <code>5655359</code> and <code>56554</code> have a common prefix <code>565</code> while <code>1223</code> and <code>43456</code> <strong>do not</strong> have a common prefix.</p>
12+
13+
<p>You need to find the length of the <strong>longest common prefix</strong> between all pairs of integers <code>(x, y)</code> such that <code>x</code> belongs to <code>arr1</code> and <code>y</code> belongs to <code>arr2</code>.</p>
14+
15+
<p>Return <em>the length of the <strong>longest</strong> common prefix among all pairs</em>.<em> If no common prefix exists among them</em>, <em>return</em> <code>0</code>.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> arr1 = [1,10,100], arr2 = [1000]
22+
<strong>Output:</strong> 3
23+
<strong>Explanation:</strong> There are 3 pairs (arr1[i], arr2[j]):
24+
- The longest common prefix of (1, 1000) is 1.
25+
- The longest common prefix of (10, 1000) is 10.
26+
- The longest common prefix of (100, 1000) is 100.
27+
The longest common prefix is 100 with a length of 3.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> arr1 = [1,2,3], arr2 = [4,4,4]
34+
<strong>Output:</strong> 0
35+
<strong>Explanation:</strong> There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0.
36+
Note that common prefixes between elements of the same array do not count.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= arr1.length, arr2.length &lt;= 5 * 10<sup>4</sup></code></li>
44+
<li><code>1 &lt;= arr1[i], arr2[i] &lt;= 10<sup>8</sup></code></li>
45+
</ul>
46+
47+
## Solutions
48+
49+
### Solution 1
50+
51+
<!-- tabs:start -->
52+
53+
```python
54+
55+
```
56+
57+
```java
58+
59+
```
60+
61+
```cpp
62+
63+
```
64+
65+
```go
66+
67+
```
68+
69+
<!-- tabs:end -->
70+
71+
<!-- end -->

0 commit comments

Comments
 (0)