File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ // my solution
4+ public class StringMatchingInArray {
5+ public List <String > stringMatching (String [] words ) {
6+ List <String > lists = new ArrayList <>();
7+ String [] tmp = words ;
8+
9+ for (int i = 0 ; i < words .length ; i ++) {
10+ for (int j = 0 ; j < words .length ; j ++) {
11+ if (tmp [i ].contains (words [j ]) && tmp [i ] != words [j ]) {
12+ if (!lists .contains (words [j ]))
13+ lists .add (words [j ]);
14+ }
15+ }
16+ }
17+ return lists ;
18+ }
19+ }
20+
21+ /* best solution
22+ class Solution {
23+ public List<String> stringMatching(String[] words) {
24+ StringBuilder sb = new StringBuilder();
25+ for(String str : words)
26+ sb.append(" ").append(str);
27+ String allstr = sb.toString();
28+
29+ List<String> result = new LinkedList<>();
30+ for(String str : words)
31+ if(allstr.indexOf(str) != allstr.lastIndexOf(str))
32+ result.add(str);
33+
34+ return result;
35+ }
36+ }
37+ */
You can’t perform that action at this time.
0 commit comments