-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringMatchingInArray.java
37 lines (32 loc) · 1016 Bytes
/
StringMatchingInArray.java
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
import java.util.*;
// my solution
public class StringMatchingInArray {
public List<String> stringMatching(String[] words) {
List<String> lists = new ArrayList<>();
String[] tmp = words;
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words.length; j++) {
if (tmp[i].contains(words[j]) && tmp[i] != words[j]) {
if (!lists.contains(words[j]))
lists.add(words[j]);
}
}
}
return lists;
}
}
/* best solution
class Solution {
public List<String> stringMatching(String[] words) {
StringBuilder sb = new StringBuilder();
for(String str : words)
sb.append(" ").append(str);
String allstr = sb.toString();
List<String> result = new LinkedList<>();
for(String str : words)
if(allstr.indexOf(str) != allstr.lastIndexOf(str))
result.add(str);
return result;
}
}
*/