Skip to content

Commit cc2c3ec

Browse files
refactor 316
1 parent 2e86b71 commit cc2c3ec

File tree

1 file changed

+5
-17
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+5
-17
lines changed

src/main/java/com/fishercoder/solutions/_316.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,15 @@
33
import java.util.ArrayDeque;
44
import java.util.Deque;
55

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-
*/
206
public class _316 {
217
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+
*/
2311
public String removeDuplicateLetters(String s) {
2412
int[] res = new int[26]; //will contain number of occurences of character (i+'a')
2513
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
2715
char[] ch = s.toCharArray();
2816
for (char c : ch) { //count number of occurences of character
2917
res[c - 'a']++;
@@ -75,7 +63,7 @@ public String removeDuplicateLetters(String s) {
7563
}
7664
}
7765
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), ""));
7967
}
8068
}
8169
}

0 commit comments

Comments
 (0)