-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path792. Number of Matching Subsequences.java
71 lines (65 loc) · 1.98 KB
/
792. Number of Matching Subsequences.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
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
64
65
66
67
68
69
70
71
class Solution {
// O N^2 Solution with Space - Constant
public int numMatchingSubseq(String s, String[] words) {
int count = 0;
for (String word : words) {
if (isSubsequence(s, word))
count++;
}
return count;
}
private boolean isSubsequence(String s, String word) {
// TODO Auto-generated method stub
int prevChIndex = 0;
for (char c : word.toCharArray()) {
int index = s.indexOf(c, prevChIndex);
if (index == -1) return false;
prevChIndex = index+1;
}
return true;
}
// ~~~~~~~ SLOW Solution ->> EXOPENTIAL COMPLEXITY + SPACE 2^n ~~~~~~~~~~
// ~~~~~~ Super BAD Super in TC ~~~~~~~~~~~~`
public int numMatchingSubseq2(String s, String[] words) {
List<String> subsequences = generateAllSubsequences(s);
System.out.println(subsequences);
Set<String> set = new HashSet<String>(subsequences);
int found = 0;
for (String word : words)
if (set.contains(word)==true) found++;
return found;
}
private List<String> generateAllSubsequences(String s) {
// TODO Auto-generated method stub
List<String> answers = new ArrayList<String>();
answers = _generateAllSubsequences(s);
return answers;
}
private List<String> _generateAllSubsequences(String s) {
// TODO Auto-generated method stub
if (s.length() == 0) {
List<String> bres = new ArrayList<String>();
bres.add("");
return bres;
}
List<String> myres = new ArrayList<String>();
char fc = s.charAt(0);
String ros = s.substring(1);
// faith
List<String> rres = _generateAllSubsequences(ros);
for (String r : rres) {
myres.add(r);
myres.add(fc + r);
}
return myres;
}
}