File tree Expand file tree Collapse file tree 1 file changed +5
-17
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +5
-17
lines changed Original file line number Diff line number Diff line change 3
3
import java .util .ArrayDeque ;
4
4
import java .util .Deque ;
5
5
6
- /**
7
- * 316. Remove Duplicate Letters
8
- *
9
- * Given a string which contains only lowercase letters,
10
- * remove duplicate letters so that every letter appear once and only once.
11
- * You must make sure your result is the smallest in lexicographical order among all possible results.
12
-
13
- Example:
14
- Given "bcabc"
15
- Return "abc"
16
-
17
- Given "cbacdcbc"
18
- Return "acdb"
19
- */
20
6
public class _316 {
21
7
public static class Solution1 {
22
- /** credit: https://discuss.leetcode.com/topic/32259/java-solution-using-stack-with-comments/2 */
8
+ /**
9
+ * credit: https://discuss.leetcode.com/topic/32259/java-solution-using-stack-with-comments/2
10
+ */
23
11
public String removeDuplicateLetters (String s ) {
24
12
int [] res = new int [26 ]; //will contain number of occurences of character (i+'a')
25
13
boolean [] visited =
26
- new boolean [26 ]; //will contain if character (i+'a') is present in current result Stack
14
+ new boolean [26 ]; //will contain if character (i+'a') is present in current result Stack
27
15
char [] ch = s .toCharArray ();
28
16
for (char c : ch ) { //count number of occurences of character
29
17
res [c - 'a' ]++;
@@ -75,7 +63,7 @@ public String removeDuplicateLetters(String s) {
75
63
}
76
64
}
77
65
return s .length () == 0 ? "" : s .charAt (pos ) + removeDuplicateLetters (
78
- s .substring (pos + 1 ).replaceAll ("" + s .charAt (pos ), "" ));
66
+ s .substring (pos + 1 ).replaceAll ("" + s .charAt (pos ), "" ));
79
67
}
80
68
}
81
69
}
You can’t perform that action at this time.
0 commit comments