-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveDuplicateLetters.java
37 lines (33 loc) · 1.1 KB
/
RemoveDuplicateLetters.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
package string;
public class RemoveDuplicateLetters {
public static void main(String[] args) {
}
public String removeDuplicateLetters(String s) {
char []chars= s.toCharArray();
boolean []used=new boolean[26];
int []count= new int[26];
for(int i=0;i<chars.length;i++){
int curr=chars[i];
count[curr-'a'] ++;
}
// Input: s = "cbacdcbc"
// Output: "acdb"
StringBuffer sb= new StringBuffer();
for(int i=0;i<chars.length;i++){
int curr=chars[i];
count[curr-'a'] --;
if(used[curr-'a']) continue;
while(sb.length()>0
&& sb.charAt(sb.length()-1)>chars[i]
&& count[sb.charAt(sb.length()-1)-'a']>0 ){
used[sb.charAt(sb.length()-1)-'a']=false;
sb.deleteCharAt(sb.length()-1);
}
sb.append(chars[i]);
used[curr-'a']=true;
// System.out.println(sb.toString());
}
// System.out.println(sb.toString());
return sb.toString();
}
}