-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubseq.java
47 lines (38 loc) · 1.26 KB
/
subseq.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
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
class Solution1 {
public static void main(String[] args) {
System.out.println(removeDuplicateLetters("ecbacba"));
}
// Greedily try to add one missing character. How to check if adding some
// character will not cause problems ? Use bit-masks to check whether you will
// be able to complete the sub-sequence if you add the character at some index
// i.
public static String removeDuplicateLetters(String s) {
List<String> list = new ArrayList<>();
int l = s.length(), lastlength = 0, cutoff = 0;
for (int i = 0; i < l; i++) {
cutoff = lastlength;
String sub = "";
char ch = s.charAt(i);
sub += ch;
for (int j = i + 1; j < l; j++) {
char c = s.charAt(j);
if (sub.indexOf(c) < 0) {
sub += c;
}
}
lastlength = sub.length();
if (lastlength >= cutoff)
list.add(sub);
else
break;
}
TreeSet<String> set=new TreeSet<>();
for (String string : list) {
set.add(string);
}
return set.first();
}
}