Skip to content

Commit be02c9c

Browse files
committed
feat: add solutions to lc problem: No.2023
No.2023.Number of Pairs of Strings With Concatenation Equal to Target
1 parent 4e7f2b0 commit be02c9c

File tree

6 files changed

+214
-26
lines changed

6 files changed

+214
-26
lines changed

solution/2000-2099/2023.Number of Pairs of Strings With Concatenation Equal to Target/README.md

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:枚举**
63+
64+
遍历数组 `nums`,对于每个 $i$,枚举所有 $j$,如果 $i \neq j$ 且 $nums[i] + nums[j] = target$,则答案加一。
65+
66+
时间复杂度 $O(n^2 \times m)$,空间复杂度 $O(1)$。其中 $n$ 和 $m$ 分别为数组 `nums` 和字符串 `target` 的长度。
67+
68+
**方法二:哈希表**
69+
70+
我们可以用哈希表统计数组 `nums` 中每个字符串出现的次数,然后遍历字符串 `target` 的所有前缀和后缀,如果前缀和后缀都在哈希表中,则答案加上它们出现的次数的乘积。
71+
72+
时间复杂度 $O(n + m^2)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别为数组 `nums` 和字符串 `target` 的长度。
73+
6274
<!-- tabs:start -->
6375

6476
### **Python3**
@@ -74,6 +86,20 @@ class Solution:
7486
)
7587
```
7688

89+
```python
90+
class Solution:
91+
def numOfPairs(self, nums: List[str], target: str) -> int:
92+
cnt = Counter(nums)
93+
ans = 0
94+
for i in range(1, len(target)):
95+
a, b = target[:i], target[i:]
96+
if a != b:
97+
ans += cnt[a] * cnt[b]
98+
else:
99+
ans += cnt[a] * (cnt[a] - 1)
100+
return ans
101+
```
102+
77103
### **Java**
78104

79105
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -95,6 +121,30 @@ class Solution {
95121
}
96122
```
97123

124+
```java
125+
class Solution {
126+
public int numOfPairs(String[] nums, String target) {
127+
Map<String, Integer> cnt = new HashMap<>();
128+
for (String x : nums) {
129+
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
130+
}
131+
int ans = 0;
132+
for (int i = 1; i < target.length(); ++i) {
133+
String a = target.substring(0, i);
134+
String b = target.substring(i);
135+
int x = cnt.getOrDefault(a, 0);
136+
int y = cnt.getOrDefault(b, 0);
137+
if (!a.equals(b)) {
138+
ans += x * y;
139+
} else {
140+
ans += x * (y - 1);
141+
}
142+
}
143+
return ans;
144+
}
145+
}
146+
```
147+
98148
### **C++**
99149

100150
```cpp
@@ -113,11 +163,32 @@ public:
113163
};
114164
```
115165
166+
```cpp
167+
class Solution {
168+
public:
169+
int numOfPairs(vector<string>& nums, string target) {
170+
unordered_map<string, int> cnt;
171+
for (auto& x : nums) ++cnt[x];
172+
int ans = 0;
173+
for (int i = 1; i < target.size(); ++i) {
174+
string a = target.substr(0, i);
175+
string b = target.substr(i);
176+
int x = cnt[a], y = cnt[b];
177+
if (a != b) {
178+
ans += x * y;
179+
} else {
180+
ans += x * (y - 1);
181+
}
182+
}
183+
return ans;
184+
}
185+
};
186+
```
187+
116188
### **Go**
117189

118190
```go
119-
func numOfPairs(nums []string, target string) int {
120-
ans := 0
191+
func numOfPairs(nums []string, target string) (ans int) {
121192
for i, a := range nums {
122193
for j, b := range nums {
123194
if i != j && a+b == target {
@@ -129,6 +200,24 @@ func numOfPairs(nums []string, target string) int {
129200
}
130201
```
131202

203+
```go
204+
func numOfPairs(nums []string, target string) (ans int) {
205+
cnt := map[string]int{}
206+
for _, x := range nums {
207+
cnt[x]++
208+
}
209+
for i := 1; i < len(target); i++ {
210+
a, b := target[:i], target[i:]
211+
if a != b {
212+
ans += cnt[a] * cnt[b]
213+
} else {
214+
ans += cnt[a] * (cnt[a] - 1)
215+
}
216+
}
217+
return
218+
}
219+
```
220+
132221
### **...**
133222

134223
```

solution/2000-2099/2023.Number of Pairs of Strings With Concatenation Equal to Target/README_EN.md

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ class Solution:
6969
)
7070
```
7171

72+
```python
73+
class Solution:
74+
def numOfPairs(self, nums: List[str], target: str) -> int:
75+
cnt = Counter(nums)
76+
ans = 0
77+
for i in range(1, len(target)):
78+
a, b = target[:i], target[i:]
79+
if a != b:
80+
ans += cnt[a] * cnt[b]
81+
else:
82+
ans += cnt[a] * (cnt[a] - 1)
83+
return ans
84+
```
85+
7286
### **Java**
7387

7488
```java
@@ -88,6 +102,30 @@ class Solution {
88102
}
89103
```
90104

105+
```java
106+
class Solution {
107+
public int numOfPairs(String[] nums, String target) {
108+
Map<String, Integer> cnt = new HashMap<>();
109+
for (String x : nums) {
110+
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
111+
}
112+
int ans = 0;
113+
for (int i = 1; i < target.length(); ++i) {
114+
String a = target.substring(0, i);
115+
String b = target.substring(i);
116+
int x = cnt.getOrDefault(a, 0);
117+
int y = cnt.getOrDefault(b, 0);
118+
if (!a.equals(b)) {
119+
ans += x * y;
120+
} else {
121+
ans += x * (y - 1);
122+
}
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
91129
### **C++**
92130

93131
```cpp
@@ -106,11 +144,32 @@ public:
106144
};
107145
```
108146
147+
```cpp
148+
class Solution {
149+
public:
150+
int numOfPairs(vector<string>& nums, string target) {
151+
unordered_map<string, int> cnt;
152+
for (auto& x : nums) ++cnt[x];
153+
int ans = 0;
154+
for (int i = 1; i < target.size(); ++i) {
155+
string a = target.substr(0, i);
156+
string b = target.substr(i);
157+
int x = cnt[a], y = cnt[b];
158+
if (a != b) {
159+
ans += x * y;
160+
} else {
161+
ans += x * (y - 1);
162+
}
163+
}
164+
return ans;
165+
}
166+
};
167+
```
168+
109169
### **Go**
110170

111171
```go
112-
func numOfPairs(nums []string, target string) int {
113-
ans := 0
172+
func numOfPairs(nums []string, target string) (ans int) {
114173
for i, a := range nums {
115174
for j, b := range nums {
116175
if i != j && a+b == target {
@@ -122,6 +181,24 @@ func numOfPairs(nums []string, target string) int {
122181
}
123182
```
124183

184+
```go
185+
func numOfPairs(nums []string, target string) (ans int) {
186+
cnt := map[string]int{}
187+
for _, x := range nums {
188+
cnt[x]++
189+
}
190+
for i := 1; i < len(target); i++ {
191+
a, b := target[:i], target[i:]
192+
if a != b {
193+
ans += cnt[a] * cnt[b]
194+
} else {
195+
ans += cnt[a] * (cnt[a] - 1)
196+
}
197+
}
198+
return
199+
}
200+
```
201+
125202
### **...**
126203

127204
```

solution/2000-2099/2023.Number of Pairs of Strings With Concatenation Equal to Target/Solution.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
class Solution {
22
public:
33
int numOfPairs(vector<string>& nums, string target) {
4-
int n = nums.size();
4+
unordered_map<string, int> cnt;
5+
for (auto& x : nums) ++cnt[x];
56
int ans = 0;
6-
for (int i = 0; i < n; ++i) {
7-
for (int j = 0; j < n; ++j) {
8-
if (i != j && nums[i] + nums[j] == target) ++ans;
7+
for (int i = 1; i < target.size(); ++i) {
8+
string a = target.substr(0, i);
9+
string b = target.substr(i);
10+
int x = cnt[a], y = cnt[b];
11+
if (a != b) {
12+
ans += x * y;
13+
} else {
14+
ans += x * (y - 1);
915
}
1016
}
1117
return ans;
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
func numOfPairs(nums []string, target string) int {
2-
ans := 0
3-
for i, a := range nums {
4-
for j, b := range nums {
5-
if i != j && a+b == target {
6-
ans++
7-
}
1+
func numOfPairs(nums []string, target string) (ans int) {
2+
cnt := map[string]int{}
3+
for _, x := range nums {
4+
cnt[x]++
5+
}
6+
for i := 1; i < len(target); i++ {
7+
a, b := target[:i], target[i:]
8+
if a != b {
9+
ans += cnt[a] * cnt[b]
10+
} else {
11+
ans += cnt[a] * (cnt[a] - 1)
812
}
913
}
10-
return ans
14+
return
1115
}

solution/2000-2099/2023.Number of Pairs of Strings With Concatenation Equal to Target/Solution.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
class Solution {
22
public int numOfPairs(String[] nums, String target) {
3-
int n = nums.length;
3+
Map<String, Integer> cnt = new HashMap<>();
4+
for (String x : nums) {
5+
cnt.put(x, cnt.getOrDefault(x, 0) + 1);
6+
}
47
int ans = 0;
5-
for (int i = 0; i < n; ++i) {
6-
for (int j = 0; j < n; ++j) {
7-
if (i != j && target.equals(nums[i] + nums[j])) {
8-
++ans;
9-
}
8+
for (int i = 1; i < target.length(); ++i) {
9+
String a = target.substring(0, i);
10+
String b = target.substring(i);
11+
int x = cnt.getOrDefault(a, 0);
12+
int y = cnt.getOrDefault(b, 0);
13+
if (!a.equals(b)) {
14+
ans += x * y;
15+
} else {
16+
ans += x * (y - 1);
1017
}
1118
}
1219
return ans;
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
class Solution:
22
def numOfPairs(self, nums: List[str], target: str) -> int:
3-
n = len(nums)
4-
return sum(
5-
i != j and nums[i] + nums[j] == target for i in range(n) for j in range(n)
6-
)
3+
cnt = Counter(nums)
4+
ans = 0
5+
for i in range(1, len(target)):
6+
a, b = target[:i], target[i:]
7+
if a != b:
8+
ans += cnt[a] * cnt[b]
9+
else:
10+
ans += cnt[a] * (cnt[a] - 1)
11+
return ans

0 commit comments

Comments
 (0)