Skip to content

Commit 3558f4e

Browse files
committed
feat: add solutions to lc problem: No.1408
No.1408.String Matching in an Array
1 parent 4f3afc1 commit 3558f4e

File tree

6 files changed

+185
-2
lines changed

6 files changed

+185
-2
lines changed

solution/1400-1499/1408.String Matching in an Array/README.md

+66-1
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,87 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:暴力枚举**
52+
5153
<!-- tabs:start -->
5254

5355
### **Python3**
5456

5557
<!-- 这里可写当前语言的特殊实现逻辑 -->
5658

5759
```python
58-
60+
class Solution:
61+
def stringMatching(self, words: List[str]) -> List[str]:
62+
ans = []
63+
for i, w1 in enumerate(words):
64+
for j, w2 in enumerate(words):
65+
if i != j and w1 in w2:
66+
ans.append(w1)
67+
break
68+
return ans
5969
```
6070

6171
### **Java**
6272

6373
<!-- 这里可写当前语言的特殊实现逻辑 -->
6474

6575
```java
76+
class Solution {
77+
public List<String> stringMatching(String[] words) {
78+
List<String> ans = new ArrayList<>();
79+
int n = words.length;
80+
for (int i = 0; i < n; ++i) {
81+
for (int j = 0; j < n; ++j) {
82+
if (i != j && words[j].contains(words[i])) {
83+
ans.add(words[i]);
84+
break;
85+
}
86+
}
87+
}
88+
return ans;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
vector<string> stringMatching(vector<string>& words) {
99+
vector<string> ans;
100+
int n = words.size();
101+
for (int i = 0; i < n; ++i)
102+
{
103+
for (int j = 0; j < n; ++j)
104+
{
105+
if (i != j && words[j].find(words[i]) != string::npos)
106+
{
107+
ans.push_back(words[i]);
108+
break;
109+
}
110+
}
111+
}
112+
return ans;
113+
}
114+
};
115+
```
66116
117+
### **Go**
118+
119+
```go
120+
func stringMatching(words []string) []string {
121+
ans := []string{}
122+
for i, w1 := range words {
123+
for j, w2 := range words {
124+
if i != j && strings.Contains(w2, w1) {
125+
ans = append(ans, w1)
126+
break
127+
}
128+
}
129+
}
130+
return ans
131+
}
67132
```
68133

69134
### **...**

solution/1400-1499/1408.String Matching in an Array/README_EN.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,76 @@
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def stringMatching(self, words: List[str]) -> List[str]:
55+
ans = []
56+
for i, w1 in enumerate(words):
57+
for j, w2 in enumerate(words):
58+
if i != j and w1 in w2:
59+
ans.append(w1)
60+
break
61+
return ans
5462
```
5563

5664
### **Java**
5765

5866
```java
67+
class Solution {
68+
public List<String> stringMatching(String[] words) {
69+
List<String> ans = new ArrayList<>();
70+
int n = words.length;
71+
for (int i = 0; i < n; ++i) {
72+
for (int j = 0; j < n; ++j) {
73+
if (i != j && words[j].contains(words[i])) {
74+
ans.add(words[i]);
75+
break;
76+
}
77+
}
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
vector<string> stringMatching(vector<string>& words) {
90+
vector<string> ans;
91+
int n = words.size();
92+
for (int i = 0; i < n; ++i)
93+
{
94+
for (int j = 0; j < n; ++j)
95+
{
96+
if (i != j && words[j].find(words[i]) != string::npos)
97+
{
98+
ans.push_back(words[i]);
99+
break;
100+
}
101+
}
102+
}
103+
return ans;
104+
}
105+
};
106+
```
59107
108+
### **Go**
109+
110+
```go
111+
func stringMatching(words []string) []string {
112+
ans := []string{}
113+
for i, w1 := range words {
114+
for j, w2 := range words {
115+
if i != j && strings.Contains(w2, w1) {
116+
ans = append(ans, w1)
117+
break
118+
}
119+
}
120+
}
121+
return ans
122+
}
60123
```
61124

62125
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<string> stringMatching(vector<string>& words) {
4+
vector<string> ans;
5+
int n = words.size();
6+
for (int i = 0; i < n; ++i)
7+
{
8+
for (int j = 0; j < n; ++j)
9+
{
10+
if (i != j && words[j].find(words[i]) != string::npos)
11+
{
12+
ans.push_back(words[i]);
13+
break;
14+
}
15+
}
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func stringMatching(words []string) []string {
2+
ans := []string{}
3+
for i, w1 := range words {
4+
for j, w2 := range words {
5+
if i != j && strings.Contains(w2, w1) {
6+
ans = append(ans, w1)
7+
break
8+
}
9+
}
10+
}
11+
return ans
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public List<String> stringMatching(String[] words) {
3+
List<String> ans = new ArrayList<>();
4+
int n = words.length;
5+
for (int i = 0; i < n; ++i) {
6+
for (int j = 0; j < n; ++j) {
7+
if (i != j && words[j].contains(words[i])) {
8+
ans.add(words[i]);
9+
break;
10+
}
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def stringMatching(self, words: List[str]) -> List[str]:
3+
ans = []
4+
for i, w1 in enumerate(words):
5+
for j, w2 in enumerate(words):
6+
if i != j and w1 in w2:
7+
ans.append(w1)
8+
break
9+
return ans

0 commit comments

Comments
 (0)