File tree 6 files changed +185
-2
lines changed
solution/1400-1499/1408.String Matching in an Array
6 files changed +185
-2
lines changed Original file line number Diff line number Diff line change 48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
+ ** 方法一:暴力枚举**
52
+
51
53
<!-- tabs:start -->
52
54
53
55
### ** Python3**
54
56
55
57
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
58
57
59
``` 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
59
69
```
60
70
61
71
### ** Java**
62
72
63
73
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
74
65
75
``` 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
+ ```
66
116
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
+ }
67
132
```
68
133
69
134
### ** ...**
Original file line number Diff line number Diff line change 50
50
### ** Python3**
51
51
52
52
``` 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
54
62
```
55
63
56
64
### ** Java**
57
65
58
66
``` 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
+ ```
59
107
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
+ }
60
123
```
61
124
62
125
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments