forked from LiMou003/test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution2.java
47 lines (45 loc) · 1.33 KB
/
Solution2.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
/**
* @description:
*
* 给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。
*
*
* 示例 1:
*
* 输入:s = "bcabc"
* 输出:"abc"
* 示例 2:
*
* 输入:s = "cbacdcbc"
* 输出:"acdb"
*
* 1 <= s.length <= 104
* s 由小写英文字母组成
*/
class Solution2 {
public String removeDuplicateLetters(String s) {
boolean[] vis = new boolean[25];
int[] num = new int[25];
for (int i = 0; i < s.length(); i++) {
num[s.charAt(i) - ' ']++;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length()+1; i++) {
char ch = s.charAt(i);
if (!vis[ch - ' ']) {
while (sb.length() > 0 && sb.charAt(sb.length() - 1) > ch) {
if (num[sb.charAt(sb.length() - 1) - 'a'] > 0) {
vis[sb.charAt(sb.length() - 1) - 'a'] = false;
sb.deleteCharAt(sb.length() - 1);
} else {
break;
}
}
vis[ch - 'a'] = true;
sb.append(ch);
}
num[ch - 'a'] += 1;
}
return sb.toString();
}
}