-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1408. String Matching in an Array
63 lines (44 loc) · 1.17 KB
/
1408. String Matching in an Array
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution
{
public static boolean func(String[] words, String curr, int index) // index here is the index to be excluded
{
for(int i = 0; i < words.length; i++)
{
if(i == index) continue;
String other = words[i];
if (other.contains(curr))
{
return true;
}
}
return false;
}
public List<String> stringMatching(String[] words)
{
ArrayList<String> result = new ArrayList<>();
for(int i = 0; i < words.length; i++)
{
String curr = words[i];
if(func(words, curr, i))
{
result.add(curr);
}
}
return result;
/* RUNTIME 4 MS , MEMORY 42.65 MB
ArrayList<String> result = new ArrayList<>();
for(String a : words)
{
for(String b : words)
{
if(a.length() < b.length() && b.indexOf(a) != -1)
{
result.add(a);
break;
}
}
}
return result;
*/
}
}