From 9c44c5cb40aa3a3a8a2b2772018819bae4a5b39a Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sun, 18 Feb 2024 06:10:31 +0000 Subject: [PATCH] feat: add weekly contest 385 --- .../README.md | 87 +++++++++++++++ .../README_EN.md | 83 +++++++++++++++ .../README.md | 75 +++++++++++++ .../README_EN.md | 71 +++++++++++++ .../3044.Most Frequent Prime/README.md | 100 ++++++++++++++++++ .../3044.Most Frequent Prime/README_EN.md | 96 +++++++++++++++++ .../3044.Most Frequent Prime/images/south | Bin 0 -> 7200 bytes .../README.md | 88 +++++++++++++++ .../README_EN.md | 84 +++++++++++++++ solution/CONTEST_README.md | 4 + solution/CONTEST_README_EN.md | 4 + solution/README.md | 6 ++ solution/README_EN.md | 6 ++ 13 files changed, 704 insertions(+) create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md create mode 100644 solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md create mode 100644 solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md create mode 100644 solution/3000-3099/3044.Most Frequent Prime/README.md create mode 100644 solution/3000-3099/3044.Most Frequent Prime/README_EN.md create mode 100644 solution/3000-3099/3044.Most Frequent Prime/images/south create mode 100644 solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README.md create mode 100644 solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README_EN.md diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md new file mode 100644 index 0000000000000..ecb370e855cd4 --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README.md @@ -0,0 +1,87 @@ +# [3042. 统计前后缀下标对 I](https://leetcode.cn/problems/count-prefix-and-suffix-pairs-i) + +[English Version](/solution/3000-3099/3042.Count%20Prefix%20and%20Suffix%20Pairs%20I/README_EN.md) + +## 题目描述 + + + +

给你一个下标从 0 开始的字符串数组 words

+ +

定义一个 布尔 函数 isPrefixAndSuffix ,它接受两个字符串参数 str1str2

+ + + +

例如,isPrefixAndSuffix("aba", "ababa") 返回 true,因为 "aba" 既是 "ababa" 的前缀,也是 "ababa" 的后缀,但是 isPrefixAndSuffix("abc", "abcd") 返回 false

+ +

以整数形式,返回满足 i < jisPrefixAndSuffix(words[i], words[j])true 的下标对 (i, j) 数量

+ +

 

+ +

示例 1:

+ +
+输入:words = ["a","aba","ababa","aa"]
+输出:4
+解释:在本示例中,计数的下标对包括:
+i = 0 且 j = 1 ,因为 isPrefixAndSuffix("a", "aba") 为 true 。
+i = 0 且 j = 2 ,因为 isPrefixAndSuffix("a", "ababa") 为 true 。
+i = 0 且 j = 3 ,因为 isPrefixAndSuffix("a", "aa") 为 true 。
+i = 1 且 j = 2 ,因为 isPrefixAndSuffix("aba", "ababa") 为 true 。
+因此,答案是 4 。
+ +

示例 2:

+ +
+输入:words = ["pa","papa","ma","mama"]
+输出:2
+解释:在本示例中,计数的下标对包括:
+i = 0 且 j = 1 ,因为 isPrefixAndSuffix("pa", "papa") 为 true 。
+i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。
+因此,答案是 2 。
+ +

示例 3:

+ +
+输入:words = ["abab","ab"]
+输出:0
+解释:在本示例中,唯一有效的下标对是 i = 0 且 j = 1 ,但是 isPrefixAndSuffix("abab", "ab") 为 false 。
+因此,答案是 0 。
+ +

 

+ +

提示:

+ + + +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md new file mode 100644 index 0000000000000..9f1ff53778c9d --- /dev/null +++ b/solution/3000-3099/3042.Count Prefix and Suffix Pairs I/README_EN.md @@ -0,0 +1,83 @@ +# [3042. Count Prefix and Suffix Pairs I](https://leetcode.com/problems/count-prefix-and-suffix-pairs-i) + +[中文文档](/solution/3000-3099/3042.Count%20Prefix%20and%20Suffix%20Pairs%20I/README.md) + +## Description + +

You are given a 0-indexed string array words.

+ +

Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2:

+ + + +

For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false.

+ +

Return an integer denoting the number of index pairs (i, j) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true.

+ +

 

+

Example 1:

+ +
+Input: words = ["a","aba","ababa","aa"]
+Output: 4
+Explanation: In this example, the counted index pairs are:
+i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
+i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
+i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
+i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
+Therefore, the answer is 4.
+ +

Example 2:

+ +
+Input: words = ["pa","papa","ma","mama"]
+Output: 2
+Explanation: In this example, the counted index pairs are:
+i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
+i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
+Therefore, the answer is 2.  
+ +

Example 3:

+ +
+Input: words = ["abab","ab"]
+Output: 0
+Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
+Therefore, the answer is 0.
+ +

 

+

Constraints:

+ + + +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md new file mode 100644 index 0000000000000..a00138baf4720 --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README.md @@ -0,0 +1,75 @@ +# [3043. 最长公共前缀的长度](https://leetcode.cn/problems/find-the-length-of-the-longest-common-prefix) + +[English Version](/solution/3000-3099/3043.Find%20the%20Length%20of%20the%20Longest%20Common%20Prefix/README_EN.md) + +## 题目描述 + + + +

给你两个 正整数 数组 arr1arr2

+ +

正整数的 前缀 是其 最左边 的一位或多位数字组成的整数。例如,123 是整数 12345 的前缀,而 234 不是

+ +

设若整数 c 是整数 ab 公共前缀 ,那么 c 需要同时是 ab 的前缀。例如,565535956554 有公共前缀 565 ,而 122343456 没有 公共前缀。

+ +

你需要找出属于 arr1 的整数 x 和属于 arr2 的整数 y 组成的所有数对 (x, y) 之中最长的公共前缀的长度。

+ +

返回所有数对之中最长公共前缀的长度。如果它们之间不存在公共前缀,则返回 0

+ +

 

+ +

示例 1:

+ +
+输入:arr1 = [1,10,100], arr2 = [1000]
+输出:3
+解释:存在 3 个数对 (arr1[i], arr2[j]) :
+- (1, 1000) 的最长公共前缀是 1 。
+- (10, 1000) 的最长公共前缀是 10 。
+- (100, 1000) 的最长公共前缀是 100 。
+最长的公共前缀是 100 ,长度为 3 。
+
+ +

示例 2:

+ +
+输入:arr1 = [1,2,3], arr2 = [4,4,4]
+输出:0
+解释:任何数对 (arr1[i], arr2[j]) 之中都不存在公共前缀,因此返回 0 。
+请注意,同一个数组内元素之间的公共前缀不在考虑范围内。
+
+ +

 

+ +

提示:

+ + + +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md new file mode 100644 index 0000000000000..e36ce785e2b80 --- /dev/null +++ b/solution/3000-3099/3043.Find the Length of the Longest Common Prefix/README_EN.md @@ -0,0 +1,71 @@ +# [3043. Find the Length of the Longest Common Prefix](https://leetcode.com/problems/find-the-length-of-the-longest-common-prefix) + +[中文文档](/solution/3000-3099/3043.Find%20the%20Length%20of%20the%20Longest%20Common%20Prefix/README.md) + +## Description + +

You are given two arrays with positive integers arr1 and arr2.

+ +

A prefix of a positive integer is an integer formed by one or more of its digits, starting from its leftmost digit. For example, 123 is a prefix of the integer 12345, while 234 is not.

+ +

A common prefix of two integers a and b is an integer c, such that c is a prefix of both a and b. For example, 5655359 and 56554 have a common prefix 565 while 1223 and 43456 do not have a common prefix.

+ +

You need to find the length of the longest common prefix between all pairs of integers (x, y) such that x belongs to arr1 and y belongs to arr2.

+ +

Return the length of the longest common prefix among all pairs. If no common prefix exists among them, return 0.

+ +

 

+

Example 1:

+ +
+Input: arr1 = [1,10,100], arr2 = [1000]
+Output: 3
+Explanation: There are 3 pairs (arr1[i], arr2[j]):
+- The longest common prefix of (1, 1000) is 1.
+- The longest common prefix of (10, 1000) is 10.
+- The longest common prefix of (100, 1000) is 100.
+The longest common prefix is 100 with a length of 3.
+
+ +

Example 2:

+ +
+Input: arr1 = [1,2,3], arr2 = [4,4,4]
+Output: 0
+Explanation: There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0.
+Note that common prefixes between elements of the same array do not count.
+
+ +

 

+

Constraints:

+ + + +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3044.Most Frequent Prime/README.md b/solution/3000-3099/3044.Most Frequent Prime/README.md new file mode 100644 index 0000000000000..6e3a8f2678c10 --- /dev/null +++ b/solution/3000-3099/3044.Most Frequent Prime/README.md @@ -0,0 +1,100 @@ +# [3044. 出现频率最高的素数](https://leetcode.cn/problems/most-frequent-prime) + +[English Version](/solution/3000-3099/3044.Most%20Frequent%20Prime/README_EN.md) + +## 题目描述 + + + +

给你一个大小为 m x n 、下标从 0 开始的二维矩阵 mat 。在每个单元格,你可以按以下方式生成数字:

+ + + +

返回在遍历矩阵所创建的所有数字中,出现频率最高的、大于 10素数;如果不存在这样的素数,则返回 -1 。如果存在多个出现频率最高的素数,那么返回其中最大的那个。

+ +

注意:移动过程中不允许改变方向。

+ +

 

+ +

示例 1:

+ + +
+
+输入:mat = [[1,1],[9,9],[1,1]]
+输出:19
+解释: 
+从单元格 (0,0) 出发,有 3 个可能的方向,这些方向上可以生成的大于 10 的数字有:
+东方向: [11], 东南方向: [19], 南方向: [19,191] 。
+从单元格 (0,1) 出发,所有可能方向上生成的大于 10 的数字有:[19,191,19,11] 。
+从单元格 (1,0) 出发,所有可能方向上生成的大于 10 的数字有:[99,91,91,91,91] 。
+从单元格 (1,1) 出发,所有可能方向上生成的大于 10 的数字有:[91,91,99,91,91] 。
+从单元格 (2,0) 出发,所有可能方向上生成的大于 10 的数字有:[11,19,191,19] 。
+从单元格 (2,1) 出发,所有可能方向上生成的大于 10 的数字有:[11,19,19,191] 。
+在所有生成的数字中,出现频率最高的素数是 19 。
+ +

示例 2:

+ +
+输入:mat = [[7]]
+输出:-1
+解释:唯一可以生成的数字是 7 。它是一个素数,但不大于 10 ,所以返回 -1 。
+ +

示例 3:

+ +
+输入:mat = [[9,7,8],[4,6,5],[2,8,6]]
+输出:97
+解释: 
+从单元格 (0,0) 出发,所有可能方向上生成的大于 10 的数字有: [97,978,96,966,94,942] 。
+从单元格 (0,1) 出发,所有可能方向上生成的大于 10 的数字有: [78,75,76,768,74,79] 。
+从单元格 (0,2) 出发,所有可能方向上生成的大于 10 的数字有: [85,856,86,862,87,879] 。
+从单元格 (1,0) 出发,所有可能方向上生成的大于 10 的数字有: [46,465,48,42,49,47] 。
+从单元格 (1,1) 出发,所有可能方向上生成的大于 10 的数字有: [65,66,68,62,64,69,67,68] 。
+从单元格 (1,2) 出发,所有可能方向上生成的大于 10 的数字有: [56,58,56,564,57,58] 。
+从单元格 (2,0) 出发,所有可能方向上生成的大于 10 的数字有: [28,286,24,249,26,268] 。
+从单元格 (2,1) 出发,所有可能方向上生成的大于 10 的数字有: [86,82,84,86,867,85] 。
+从单元格 (2,2) 出发,所有可能方向上生成的大于 10 的数字有: [68,682,66,669,65,658] 。
+在所有生成的数字中,出现频率最高的素数是 97 。
+
+ +

 

+ +

提示:

+ + + +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3044.Most Frequent Prime/README_EN.md b/solution/3000-3099/3044.Most Frequent Prime/README_EN.md new file mode 100644 index 0000000000000..561f6eba582a8 --- /dev/null +++ b/solution/3000-3099/3044.Most Frequent Prime/README_EN.md @@ -0,0 +1,96 @@ +# [3044. Most Frequent Prime](https://leetcode.com/problems/most-frequent-prime) + +[中文文档](/solution/3000-3099/3044.Most%20Frequent%20Prime/README.md) + +## Description + +

You are given a m x n 0-indexed 2D matrix mat. From every cell, you can create numbers in the following way:

+ + + +

Return the most frequent prime number greater than 10 out of all the numbers created by traversing the matrix or -1 if no such prime number exists. If there are multiple prime numbers with the highest frequency, then return the largest among them.

+ +

Note: It is invalid to change the direction during the move.

+ +

 

+

Example 1:

+ + +
+
+Input: mat = [[1,1],[9,9],[1,1]]
+Output: 19
+Explanation: 
+From cell (0,0) there are 3 possible directions and the numbers greater than 10 which can be created in those directions are:
+East: [11], South-East: [19], South: [19,191].
+Numbers greater than 10 created from the cell (0,1) in all possible directions are: [19,191,19,11].
+Numbers greater than 10 created from the cell (1,0) in all possible directions are: [99,91,91,91,91].
+Numbers greater than 10 created from the cell (1,1) in all possible directions are: [91,91,99,91,91].
+Numbers greater than 10 created from the cell (2,0) in all possible directions are: [11,19,191,19].
+Numbers greater than 10 created from the cell (2,1) in all possible directions are: [11,19,19,191].
+The most frequent prime number among all the created numbers is 19.
+ +

Example 2:

+ +
+Input: mat = [[7]]
+Output: -1
+Explanation: The only number which can be formed is 7. It is a prime number however it is not greater than 10, so return -1.
+ +

Example 3:

+ +
+Input: mat = [[9,7,8],[4,6,5],[2,8,6]]
+Output: 97
+Explanation: 
+Numbers greater than 10 created from the cell (0,0) in all possible directions are: [97,978,96,966,94,942].
+Numbers greater than 10 created from the cell (0,1) in all possible directions are: [78,75,76,768,74,79].
+Numbers greater than 10 created from the cell (0,2) in all possible directions are: [85,856,86,862,87,879].
+Numbers greater than 10 created from the cell (1,0) in all possible directions are: [46,465,48,42,49,47].
+Numbers greater than 10 created from the cell (1,1) in all possible directions are: [65,66,68,62,64,69,67,68].
+Numbers greater than 10 created from the cell (1,2) in all possible directions are: [56,58,56,564,57,58].
+Numbers greater than 10 created from the cell (2,0) in all possible directions are: [28,286,24,249,26,268].
+Numbers greater than 10 created from the cell (2,1) in all possible directions are: [86,82,84,86,867,85].
+Numbers greater than 10 created from the cell (2,2) in all possible directions are: [68,682,66,669,65,658].
+The most frequent prime number among all the created numbers is 97.
+
+ +

 

+

Constraints:

+ + + +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3044.Most Frequent Prime/images/south b/solution/3000-3099/3044.Most Frequent Prime/images/south new file mode 100644 index 0000000000000000000000000000000000000000..456a293a352d7e848f8fd95d7190f27718f058b4 GIT binary patch literal 7200 zcmb_h2{hF2-v41@l4WR7{7H<+T8WBmm1Pp8vTs?E>`V4#1_>b`R5Q@7pjl?=#hX-+Rw_-*fM|@11iTGr!+6&+}bBpU?L<5!bcVS(&++AqZkcXz+1h=+i>ta8iy#r&{e)UCU`jmuLmxq2lJ zOFg0%jDqRkJvXZt`YtQaaBDB;LU=T5CS$$? zx-}#H8Cwp6jCf5$kplQ{VU=iEXe;Eo=?HocCV;(c9$mU-?E{N)eLk)k4 z1ts`<3*Ru#k0UYzQbr_sf1LD!VXrIT$b`HhWggi9#oB)g=kBbfs> zs7C!LG}EqqW3ni6md?bX{iT9YwRf?sLtomRmINH1_V<%$m)x3l!b;9Kh~<4b)7Ko0 zJmh_eIZE=V7x=!=I;~{c>G80mo(fS7mXtB97T9#!yi%3ikNUp*$;oT!!QTDWQL(4g zt|_d?&yFM6fRPTfMTLdh{SLi@ruA!5Ca)=TNLPv_7d_pUM2dEp$`fZUI!sr@@WO4# z_6RJKx+uyBT@FLQ`4d2#t{cshl0tgr#zY&OJ zgbD~Su|W>TsKSq$=|0h0A0+{News(|-y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``hPd_%y3xSfUf zk&gw8-riQp-^t0zIY|Tf7`1^a4}0&!B=8LR%f@HH_=<#V*POgOJKfFPTVF10^jWE9 z2W^2+SpmH6oGCA{X+3?n{Uxh&1t8Pc>Ct{`M2o=f7W3zMd7{#{>oA%dH^y!8<&O`5 z+5a>^PyGw3EQ5^vz5slymp<;Zm>&fZvsoCz*??a@CSQf|F~@YJ$T}MJu>)DW&yN_& zWp;AkA#r%cK2Xy|9|r-;c50kXy4v_PNm_W-1GU+64a%8 zCex~8Hxg+BGHCCl6nU>hrg--$?bRYQS!{P3^ry{mm4{6;R>+1-TAw6#raO+9gC{rV zJ?9SZd3f2*u!V+{K;B4eCQ8cC{y<$YUmJDF9WXDp45euou(VIexx#r=@mpV?_oor@ zwHtnZgBu?~3sfWV_wJv$Bg9q7hY@r2yYb$0erDc1=;tHjqfZ6PSw*-cD!4$%dVm6{ z7sTY}<#lIYJ8J6QD&t5Q4Pj!pYkJIE1s*r_fU5yN1&`ia^>nV?&Z0fz(bd;C_Y`Mh z=j-+$n0htu;0$mZ7KVyB`5YBoMQ#!&<12paPK;aZ&v{kc#xZp!@Wj$PM?6jfg2K7< gXtU#g_;q;-o%Wo+K)JsQL)#gps(rce;_cx70{A2sv;Y7A literal 0 HcmV?d00001 diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README.md b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README.md new file mode 100644 index 0000000000000..036ce4dc1404f --- /dev/null +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README.md @@ -0,0 +1,88 @@ +# [3045. 统计前后缀下标对 II](https://leetcode.cn/problems/count-prefix-and-suffix-pairs-ii) + +[English Version](/solution/3000-3099/3045.Count%20Prefix%20and%20Suffix%20Pairs%20II/README_EN.md) + +## 题目描述 + + + +

给你一个下标从 0 开始的字符串数组 words

+ +

定义一个 布尔 函数 isPrefixAndSuffix ,它接受两个字符串参数 str1str2

+ +
    +
  • str1 同时是 str2 的前缀(prefix)和后缀(suffix)时,isPrefixAndSuffix(str1, str2) 返回 true,否则返回 false
  • +
+ +

例如,isPrefixAndSuffix("aba", "ababa") 返回 true,因为 "aba" 既是 "ababa" 的前缀,也是 "ababa" 的后缀,但是 isPrefixAndSuffix("abc", "abcd") 返回 false

+ +

以整数形式,返回满足 i < jisPrefixAndSuffix(words[i], words[j])true 的下标对 (i, j) 数量

+ +

 

+ +

示例 1:

+ +
+输入:words = ["a","aba","ababa","aa"]
+输出:4
+解释:在本示例中,计数的下标对包括:
+i = 0 且 j = 1 ,因为 isPrefixAndSuffix("a", "aba") 为 true 。
+i = 0 且 j = 2 ,因为 isPrefixAndSuffix("a", "ababa") 为 true 。
+i = 0 且 j = 3 ,因为 isPrefixAndSuffix("a", "aa") 为 true 。
+i = 1 且 j = 2 ,因为 isPrefixAndSuffix("aba", "ababa") 为 true 。
+因此,答案是 4 。
+ +

示例 2:

+ +
+输入:words = ["pa","papa","ma","mama"]
+输出:2
+解释:在本示例中,计数的下标对包括:
+i = 0 且 j = 1 ,因为 isPrefixAndSuffix("pa", "papa") 为 true 。
+i = 2 且 j = 3 ,因为 isPrefixAndSuffix("ma", "mama") 为 true 。
+因此,答案是 2 。
+ +

示例 3:

+ +
+输入:words = ["abab","ab"]
+输出:0
+解释:在本示例中,唯一有效的下标对是 i = 0 且 j = 1 ,但是 isPrefixAndSuffix("abab", "ab") 为 false 。
+因此,答案是 0 。
+ +

 

+ +

提示:

+ +
    +
  • 1 <= words.length <= 105
  • +
  • 1 <= words[i].length <= 105
  • +
  • words[i] 仅由小写英文字母组成。
  • +
  • 所有 words[i] 的长度之和不超过 5 * 105
  • +
+ +## 解法 + +### 方法一 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README_EN.md b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README_EN.md new file mode 100644 index 0000000000000..6fe5f0b4044ac --- /dev/null +++ b/solution/3000-3099/3045.Count Prefix and Suffix Pairs II/README_EN.md @@ -0,0 +1,84 @@ +# [3045. Count Prefix and Suffix Pairs II](https://leetcode.com/problems/count-prefix-and-suffix-pairs-ii) + +[中文文档](/solution/3000-3099/3045.Count%20Prefix%20and%20Suffix%20Pairs%20II/README.md) + +## Description + +

You are given a 0-indexed string array words.

+ +

Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2:

+ +
    +
  • isPrefixAndSuffix(str1, str2) returns true if str1 is both a prefix and a suffix of str2, and false otherwise.
  • +
+ +

For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false.

+ +

Return an integer denoting the number of index pairs (i, j) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true.

+ +

 

+

Example 1:

+ +
+Input: words = ["a","aba","ababa","aa"]
+Output: 4
+Explanation: In this example, the counted index pairs are:
+i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
+i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
+i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
+i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
+Therefore, the answer is 4.
+ +

Example 2:

+ +
+Input: words = ["pa","papa","ma","mama"]
+Output: 2
+Explanation: In this example, the counted index pairs are:
+i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
+i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
+Therefore, the answer is 2.  
+ +

Example 3:

+ +
+Input: words = ["abab","ab"]
+Output: 0
+Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
+Therefore, the answer is 0.
+ +

 

+

Constraints:

+ +
    +
  • 1 <= words.length <= 105
  • +
  • 1 <= words[i].length <= 105
  • +
  • words[i] consists only of lowercase English letters.
  • +
  • The sum of the lengths of all words[i] does not exceed 5 * 105.
  • +
+ +## Solutions + +### Solution 1 + + + +```python + +``` + +```java + +``` + +```cpp + +``` + +```go + +``` + + + + diff --git a/solution/CONTEST_README.md b/solution/CONTEST_README.md index 1d8f493dd6dbb..5de0999552d5f 100644 --- a/solution/CONTEST_README.md +++ b/solution/CONTEST_README.md @@ -24,6 +24,10 @@ #### 第 385 场周赛(2024-02-18 10:30, 90 分钟) 参赛人数 2352 +- [3042. 统计前后缀下标对 I](/solution/3000-3099/3042.Count%20Prefix%20and%20Suffix%20Pairs%20I/README.md) +- [3043. 最长公共前缀的长度](/solution/3000-3099/3043.Find%20the%20Length%20of%20the%20Longest%20Common%20Prefix/README.md) +- [3044. 出现频率最高的素数](/solution/3000-3099/3044.Most%20Frequent%20Prime/README.md) +- [3045. 统计前后缀下标对 II](/solution/3000-3099/3045.Count%20Prefix%20and%20Suffix%20Pairs%20II/README.md) #### 第 124 场双周赛(2024-02-17 22:30, 90 分钟) 参赛人数 1861 diff --git a/solution/CONTEST_README_EN.md b/solution/CONTEST_README_EN.md index 070d3e15283d3..464ee769300af 100644 --- a/solution/CONTEST_README_EN.md +++ b/solution/CONTEST_README_EN.md @@ -27,6 +27,10 @@ Get your rating changes right after the completion of LeetCode contests, https:/ #### Weekly Contest 385 +- [3042. Count Prefix and Suffix Pairs I](/solution/3000-3099/3042.Count%20Prefix%20and%20Suffix%20Pairs%20I/README_EN.md) +- [3043. Find the Length of the Longest Common Prefix](/solution/3000-3099/3043.Find%20the%20Length%20of%20the%20Longest%20Common%20Prefix/README_EN.md) +- [3044. Most Frequent Prime](/solution/3000-3099/3044.Most%20Frequent%20Prime/README_EN.md) +- [3045. Count Prefix and Suffix Pairs II](/solution/3000-3099/3045.Count%20Prefix%20and%20Suffix%20Pairs%20II/README_EN.md) #### Biweekly Contest 124 diff --git a/solution/README.md b/solution/README.md index b044e9c12dc3b..bca021dc2a6bb 100644 --- a/solution/README.md +++ b/solution/README.md @@ -1125,6 +1125,8 @@ | 1112 | [每位学生的最高成绩](/solution/1100-1199/1112.Highest%20Grade%20For%20Each%20Student/README.md) | `数据库` | 中等 | 🔒 | | 1113 | [报告的记录](/solution/1100-1199/1113.Reported%20Posts/README.md) | `数据库` | 简单 | 🔒 | | 1114 | [按序打印](/solution/1100-1199/1114.Print%20in%20Order/README.md) | `多线程` | 简单 | | +| 1115 | [交替打印 FooBar](/solution/1100-1199/1115.Print%20FooBar%20Alternately/README.md) | `多线程` | 中等 | | +| 1116 | [打印零与奇偶数](/solution/1100-1199/1116.Print%20Zero%20Even%20Odd/README.md) | `多线程` | 中等 | | | 1117 | [H2O 生成](/solution/1100-1199/1117.Building%20H2O/README.md) | `多线程` | 中等 | | | 1118 | [一月有多少天](/solution/1100-1199/1118.Number%20of%20Days%20in%20a%20Month/README.md) | `数学` | 简单 | 第 4 场双周赛 | | 1119 | [删去字符串中的元音](/solution/1100-1199/1119.Remove%20Vowels%20from%20a%20String/README.md) | `字符串` | 简单 | 第 4 场双周赛 | @@ -3049,6 +3051,10 @@ | 3039 | [进行操作使字符串为空](/solution/3000-3099/3039.Apply%20Operations%20to%20Make%20String%20Empty/README.md) | | 中等 | 第 124 场双周赛 | | 3040 | [相同分数的最大操作数目 II](/solution/3000-3099/3040.Maximum%20Number%20of%20Operations%20With%20the%20Same%20Score%20II/README.md) | | 中等 | 第 124 场双周赛 | | 3041 | [修改数组后最大化数组中的连续元素数目](/solution/3000-3099/3041.Maximize%20Consecutive%20Elements%20in%20an%20Array%20After%20Modification/README.md) | | 困难 | 第 124 场双周赛 | +| 3042 | [统计前后缀下标对 I](/solution/3000-3099/3042.Count%20Prefix%20and%20Suffix%20Pairs%20I/README.md) | | 简单 | 第 385 场周赛 | +| 3043 | [最长公共前缀的长度](/solution/3000-3099/3043.Find%20the%20Length%20of%20the%20Longest%20Common%20Prefix/README.md) | | 中等 | 第 385 场周赛 | +| 3044 | [出现频率最高的素数](/solution/3000-3099/3044.Most%20Frequent%20Prime/README.md) | | 中等 | 第 385 场周赛 | +| 3045 | [统计前后缀下标对 II](/solution/3000-3099/3045.Count%20Prefix%20and%20Suffix%20Pairs%20II/README.md) | | 困难 | 第 385 场周赛 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 494ca17512161..19dc9a5160841 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -1123,6 +1123,8 @@ Press Control + F(or Command + F on | 1112 | [Highest Grade For Each Student](/solution/1100-1199/1112.Highest%20Grade%20For%20Each%20Student/README_EN.md) | `Database` | Medium | 🔒 | | 1113 | [Reported Posts](/solution/1100-1199/1113.Reported%20Posts/README_EN.md) | `Database` | Easy | 🔒 | | 1114 | [Print in Order](/solution/1100-1199/1114.Print%20in%20Order/README_EN.md) | `Concurrency` | Easy | | +| 1115 | [Print FooBar Alternately](/solution/1100-1199/1115.Print%20FooBar%20Alternately/README_EN.md) | `Concurrency` | Medium | | +| 1116 | [Print Zero Even Odd](/solution/1100-1199/1116.Print%20Zero%20Even%20Odd/README_EN.md) | `Concurrency` | Medium | | | 1117 | [Building H2O](/solution/1100-1199/1117.Building%20H2O/README_EN.md) | `Concurrency` | Medium | | | 1118 | [Number of Days in a Month](/solution/1100-1199/1118.Number%20of%20Days%20in%20a%20Month/README_EN.md) | `Math` | Easy | Biweekly Contest 4 | | 1119 | [Remove Vowels from a String](/solution/1100-1199/1119.Remove%20Vowels%20from%20a%20String/README_EN.md) | `String` | Easy | Biweekly Contest 4 | @@ -3047,6 +3049,10 @@ Press Control + F(or Command + F on | 3039 | [Apply Operations to Make String Empty](/solution/3000-3099/3039.Apply%20Operations%20to%20Make%20String%20Empty/README_EN.md) | | Medium | Biweekly Contest 124 | | 3040 | [Maximum Number of Operations With the Same Score II](/solution/3000-3099/3040.Maximum%20Number%20of%20Operations%20With%20the%20Same%20Score%20II/README_EN.md) | | Medium | Biweekly Contest 124 | | 3041 | [Maximize Consecutive Elements in an Array After Modification](/solution/3000-3099/3041.Maximize%20Consecutive%20Elements%20in%20an%20Array%20After%20Modification/README_EN.md) | | Hard | Biweekly Contest 124 | +| 3042 | [Count Prefix and Suffix Pairs I](/solution/3000-3099/3042.Count%20Prefix%20and%20Suffix%20Pairs%20I/README_EN.md) | | Easy | Weekly Contest 385 | +| 3043 | [Find the Length of the Longest Common Prefix](/solution/3000-3099/3043.Find%20the%20Length%20of%20the%20Longest%20Common%20Prefix/README_EN.md) | | Medium | Weekly Contest 385 | +| 3044 | [Most Frequent Prime](/solution/3000-3099/3044.Most%20Frequent%20Prime/README_EN.md) | | Medium | Weekly Contest 385 | +| 3045 | [Count Prefix and Suffix Pairs II](/solution/3000-3099/3045.Count%20Prefix%20and%20Suffix%20Pairs%20II/README_EN.md) | | Hard | Weekly Contest 385 | ## Copyright