-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm1408.java
More file actions
42 lines (38 loc) · 1.32 KB
/
prblm1408.java
File metadata and controls
42 lines (38 loc) · 1.32 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
41
42
import java.util.*;
public class prblm1408 {
public static void main(String[] args) {
String[] words = {"mass","as","hero","superhero"};
List<String> ans = stringMatching(words);
System.out.println(ans);
String[] words2 = {"leetcoder","leetcode","od","hamlet","am"};
List<String> ans2 = stringMatching2(words2);
System.out.println(ans2);
}
public static List<String> stringMatching(String[] words) {
List<String> ans = new ArrayList<>();
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words.length; j++) {
if (!words[i].equals(words[j])) {
if (words[j].contains(words[i])) {
if (!ans.contains(words[i])) {
ans.add(words[i]);
}
}
}
}
}
return ans;
}
public static List<String> stringMatching2(String[] words) {
List<String> ans = new ArrayList<>();
for(int i = 0; i < words.length; i++){
for(int j = 0; j < words.length; j++){
if(i != j && words[j].contains(words[i])){
ans.add(words[i]);
break;
}
}
}
return ans;
}
}