-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMatchingInArray.java
More file actions
36 lines (27 loc) · 979 Bytes
/
Copy pathStringMatchingInArray.java
File metadata and controls
36 lines (27 loc) · 979 Bytes
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
package LeetcodePractice;
import java.util.*;
public class StringMatchingInArray {
public static void main(String[] args) {
String[] words1 = {"mass","as","hero","superhero"};
System.out.println(stringMatching(words1));
String[] words2 = {"leetcode","et","code"};
System.out.println(stringMatching(words2));
String[] words3 = {"blue","green","bu"};
System.out.println(stringMatching(words3));
}
public static List<String> stringMatching(String[] words) {
List<String> result = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
String word1 = words[i];
for (int j = 0; j < words.length; j++) {
if (i == j) continue;
String word2 = words[j];
if (word2.contains(word1)) {
result.add(word1);
break;
}
}
}
return result;
}
}