-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
63 lines (63 loc) · 2.03 KB
/
Solution.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
public class Solution {
List<List<Integer>> totalList=new ArrayList<List<Integer>>();
int index=-1;
public String removeDuplicateLetters(String s) {
if (s==null||s.length()==0) {
return "";
}
char[] chars=s.toCharArray();
for (int i = 0; i < 26; i++) {
List<Integer> list=new ArrayList<Integer>();
totalList.add(list);
}
for (int i = 0; i < chars.length; i++) {
int k=chars[i]-97;
totalList.get(k).add(i);
}
int count=0;
for (int i = 0; i < 26; i++) {
if (totalList.get(i).size()!=0) {
count++;
}
}
char[] ans=new char[count];
for (int i = 0; i < count; i++) {
ans[i]=f();
}
return String.valueOf(ans);
}
public char f(){
int min=Integer.MAX_VALUE;
for (int i = 0; i < 26; i++) {
if (totalList.get(i).size()!=0) {
int k=totalList.get(i).get(totalList.get(i).size()-1);
if (k<min) {
min=k;
}
}
}
for (int i = 0; i < 26; i++) {
if (totalList.get(i).size()!=0) {
int val=totalList.get(i).get(0);
if (val<=min&&val>index) {
totalList.set(i, new ArrayList<Integer>());
index=val;
for (int j = 0; j < 26; j++) {
if (totalList.get(j).size()!=0) {
List<Integer> list=totalList.get(j);
for (int k = 0; k < list.size();) {
if (list.get(0)<index) {
list.remove(0);
}else {
break;
}
}
}
}
return (char)(i+97);
}
}
}
return 0;
}
}