-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMatchingInAnArray.java
More file actions
40 lines (35 loc) · 1.13 KB
/
StringMatchingInAnArray.java
File metadata and controls
40 lines (35 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* StringMatchingInAnArray
* https://leetcode.cn/problems/string-matching-in-an-array/
* 1408. 数组中的字符串匹配
* https://leetcode.cn/problems/string-matching-in-an-array/solution/bao-li-sou-suo-pan-duan-by-oshdyr-f81q/
*
* @author tobin
* @since 2022-08-06
*/
public class StringMatchingInAnArray {
public static void main(String[] args) {
StringMatchingInAnArray sol = new StringMatchingInAnArray();
List<String> res = sol.stringMatching(new String[]{"mass", "as", "hero", "superhero"});
for (String str : res) {
System.out.println(str);
}
}
public List<String> stringMatching(String[] words) {
List<String> result = new LinkedList<>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words.length; j++) {
if (i != j) {
if (words[j].contains(words[i])) {
result.add(words[i]);
break;
}
}
}
}
return result;
}
}