Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1408
Browse files Browse the repository at this point in the history
No.1408.String Matching in an Array
  • Loading branch information
yanglbme committed Aug 5, 2022
1 parent 4f3afc1 commit 3558f4e
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 2 deletions.
67 changes: 66 additions & 1 deletion solution/1400-1499/1408.String Matching in an Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,87 @@

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

**方法一:暴力枚举**

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
ans = []
for i, w1 in enumerate(words):
for j, w2 in enumerate(words):
if i != j and w1 in w2:
ans.append(w1)
break
return ans
```

### **Java**

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

```java
class Solution {
public List<String> stringMatching(String[] words) {
List<String> ans = new ArrayList<>();
int n = words.length;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && words[j].contains(words[i])) {
ans.add(words[i]);
break;
}
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
vector<string> ans;
int n = words.size();
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (i != j && words[j].find(words[i]) != string::npos)
{
ans.push_back(words[i]);
break;
}
}
}
return ans;
}
};
```
### **Go**
```go
func stringMatching(words []string) []string {
ans := []string{}
for i, w1 := range words {
for j, w2 := range words {
if i != j && strings.Contains(w2, w1) {
ans = append(ans, w1)
break
}
}
}
return ans
}
```

### **...**
Expand Down
65 changes: 64 additions & 1 deletion solution/1400-1499/1408.String Matching in an Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,76 @@
### **Python3**

```python

class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
ans = []
for i, w1 in enumerate(words):
for j, w2 in enumerate(words):
if i != j and w1 in w2:
ans.append(w1)
break
return ans
```

### **Java**

```java
class Solution {
public List<String> stringMatching(String[] words) {
List<String> ans = new ArrayList<>();
int n = words.length;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && words[j].contains(words[i])) {
ans.add(words[i]);
break;
}
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
vector<string> ans;
int n = words.size();
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (i != j && words[j].find(words[i]) != string::npos)
{
ans.push_back(words[i]);
break;
}
}
}
return ans;
}
};
```
### **Go**
```go
func stringMatching(words []string) []string {
ans := []string{}
for i, w1 := range words {
for j, w2 := range words {
if i != j && strings.Contains(w2, w1) {
ans = append(ans, w1)
break
}
}
}
return ans
}
```

### **...**
Expand Down
19 changes: 19 additions & 0 deletions solution/1400-1499/1408.String Matching in an Array/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
vector<string> stringMatching(vector<string>& words) {
vector<string> ans;
int n = words.size();
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (i != j && words[j].find(words[i]) != string::npos)
{
ans.push_back(words[i]);
break;
}
}
}
return ans;
}
};
12 changes: 12 additions & 0 deletions solution/1400-1499/1408.String Matching in an Array/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func stringMatching(words []string) []string {
ans := []string{}
for i, w1 := range words {
for j, w2 := range words {
if i != j && strings.Contains(w2, w1) {
ans = append(ans, w1)
break
}
}
}
return ans
}
15 changes: 15 additions & 0 deletions solution/1400-1499/1408.String Matching in an Array/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public List<String> stringMatching(String[] words) {
List<String> ans = new ArrayList<>();
int n = words.length;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && words[j].contains(words[i])) {
ans.add(words[i]);
break;
}
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
ans = []
for i, w1 in enumerate(words):
for j, w2 in enumerate(words):
if i != j and w1 in w2:
ans.append(w1)
break
return ans

0 comments on commit 3558f4e

Please sign in to comment.