|
18 | 18 | Return "acdb"
|
19 | 19 | */
|
20 | 20 | public class _316 {
|
21 |
| - /**credit: https://discuss.leetcode.com/topic/32259/java-solution-using-stack-with-comments/2*/ |
22 |
| - public String removeDuplicateLetters_use_stack(String s) { |
23 |
| - int[] res = new int[26]; //will contain number of occurences of character (i+'a') |
24 |
| - boolean[] visited = new boolean[26]; //will contain if character (i+'a') is present in current result Stack |
25 |
| - char[] ch = s.toCharArray(); |
26 |
| - for (char c : ch) { //count number of occurences of character |
27 |
| - res[c - 'a']++; |
28 |
| - } |
29 |
| - Deque<Character> st = new ArrayDeque<>(); // answer stack |
30 |
| - int index; |
31 |
| - for (char c : ch) { |
32 |
| - index = c - 'a'; |
33 |
| - res[index]--; //decrement number of characters remaining in the string to be analysed |
34 |
| - if (visited[index]) { |
35 |
| - //if character is already present in stack, dont bother |
36 |
| - continue; |
| 21 | + public static class Solution1 { |
| 22 | + /** credit: https://discuss.leetcode.com/topic/32259/java-solution-using-stack-with-comments/2 */ |
| 23 | + public String removeDuplicateLetters(String s) { |
| 24 | + int[] res = new int[26]; //will contain number of occurences of character (i+'a') |
| 25 | + boolean[] visited = |
| 26 | + new boolean[26]; //will contain if character (i+'a') is present in current result Stack |
| 27 | + char[] ch = s.toCharArray(); |
| 28 | + for (char c : ch) { //count number of occurences of character |
| 29 | + res[c - 'a']++; |
37 | 30 | }
|
38 |
| - //if current character is smaller than last character in stack which occurs later in the string again |
39 |
| - //it can be removed and added later e.g stack = bc remaining string abc then a can pop b and then c |
40 |
| - while (!st.isEmpty() && c < st.peek() && res[st.peek() - 'a'] != 0) { |
41 |
| - visited[st.pop() - 'a'] = false; |
| 31 | + Deque<Character> st = new ArrayDeque<>(); // answer stack |
| 32 | + int index; |
| 33 | + for (char c : ch) { |
| 34 | + index = c - 'a'; |
| 35 | + res[index]--; //decrement number of characters remaining in the string to be analysed |
| 36 | + if (visited[index]) { |
| 37 | + //if character is already present in stack, dont bother |
| 38 | + continue; |
| 39 | + } |
| 40 | + //if current character is smaller than last character in stack which occurs later in the string again |
| 41 | + //it can be removed and added later e.g stack = bc remaining string abc then a can pop b and then c |
| 42 | + while (!st.isEmpty() && c < st.peek() && res[st.peek() - 'a'] != 0) { |
| 43 | + visited[st.pop() - 'a'] = false; |
| 44 | + } |
| 45 | + st.push(c); //add current character and mark it as visited |
| 46 | + visited[index] = true; |
42 | 47 | }
|
43 |
| - st.push(c); //add current character and mark it as visited |
44 |
| - visited[index] = true; |
45 |
| - } |
46 | 48 |
|
47 |
| - StringBuilder sb = new StringBuilder(); |
48 |
| - //pop character from stack and build answer string from back |
49 |
| - while (!st.isEmpty()) { |
50 |
| - sb.insert(0, st.pop()); |
| 49 | + StringBuilder sb = new StringBuilder(); |
| 50 | + //pop character from stack and build answer string from back |
| 51 | + while (!st.isEmpty()) { |
| 52 | + sb.insert(0, st.pop()); |
| 53 | + } |
| 54 | + return sb.toString(); |
51 | 55 | }
|
52 |
| - return sb.toString(); |
53 | 56 | }
|
54 | 57 |
|
55 |
| - /** |
56 |
| - * Credit: https://discuss.leetcode.com/topic/31404/a-short-o-n-recursive-greedy-solution |
57 |
| - */ |
58 |
| - public String removeDuplicateLetters(String s) { |
59 |
| - int[] count = new int[26]; |
60 |
| - int pos = 0; // the position for the smallest s[i] |
61 |
| - for (int i = 0; i < s.length(); i++) { |
62 |
| - count[s.charAt(i) - 'a']++; |
63 |
| - } |
64 |
| - for (int i = 0; i < s.length(); i++) { |
65 |
| - if (s.charAt(i) < s.charAt(pos)) { |
66 |
| - pos = i; |
| 58 | + public static class Solution2 { |
| 59 | + /** |
| 60 | + * Credit: https://discuss.leetcode.com/topic/31404/a-short-o-n-recursive-greedy-solution |
| 61 | + */ |
| 62 | + public String removeDuplicateLetters(String s) { |
| 63 | + int[] count = new int[26]; |
| 64 | + int pos = 0; // the position for the smallest s[i] |
| 65 | + for (int i = 0; i < s.length(); i++) { |
| 66 | + count[s.charAt(i) - 'a']++; |
67 | 67 | }
|
68 |
| - count[s.charAt(i) - 'a']--; |
69 |
| - if (count[s.charAt(i) - 'a'] == 0) { |
70 |
| - break; |
| 68 | + for (int i = 0; i < s.length(); i++) { |
| 69 | + if (s.charAt(i) < s.charAt(pos)) { |
| 70 | + pos = i; |
| 71 | + } |
| 72 | + count[s.charAt(i) - 'a']--; |
| 73 | + if (count[s.charAt(i) - 'a'] == 0) { |
| 74 | + break; |
| 75 | + } |
71 | 76 | }
|
| 77 | + return s.length() == 0 ? "" : s.charAt(pos) + removeDuplicateLetters( |
| 78 | + s.substring(pos + 1).replaceAll("" + s.charAt(pos), "")); |
72 | 79 | }
|
73 |
| - return s.length() == 0 ? "" : s.charAt(pos) + removeDuplicateLetters(s.substring(pos + 1).replaceAll("" + s.charAt(pos), "")); |
74 | 80 | }
|
75 | 81 | }
|
0 commit comments