forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_316.java
81 lines (75 loc) · 3.1 KB
/
_316.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.fishercoder.solutions;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* 316. Remove Duplicate Letters
*
* Given a string which contains only lowercase letters,
* remove duplicate letters so that every letter appear once and only once.
* You must make sure your result is the smallest in lexicographical order among all possible results.
Example:
Given "bcabc"
Return "abc"
Given "cbacdcbc"
Return "acdb"
*/
public class _316 {
public static class Solution1 {
/** credit: https://discuss.leetcode.com/topic/32259/java-solution-using-stack-with-comments/2 */
public String removeDuplicateLetters(String s) {
int[] res = new int[26]; //will contain number of occurences of character (i+'a')
boolean[] visited =
new boolean[26]; //will contain if character (i+'a') is present in current result Stack
char[] ch = s.toCharArray();
for (char c : ch) { //count number of occurences of character
res[c - 'a']++;
}
Deque<Character> st = new ArrayDeque<>(); // answer stack
int index;
for (char c : ch) {
index = c - 'a';
res[index]--; //decrement number of characters remaining in the string to be analysed
if (visited[index]) {
//if character is already present in stack, dont bother
continue;
}
//if current character is smaller than last character in stack which occurs later in the string again
//it can be removed and added later e.g stack = bc remaining string abc then a can pop b and then c
while (!st.isEmpty() && c < st.peek() && res[st.peek() - 'a'] != 0) {
visited[st.pop() - 'a'] = false;
}
st.push(c); //add current character and mark it as visited
visited[index] = true;
}
StringBuilder sb = new StringBuilder();
//pop character from stack and build answer string from back
while (!st.isEmpty()) {
sb.insert(0, st.pop());
}
return sb.toString();
}
}
public static class Solution2 {
/**
* Credit: https://discuss.leetcode.com/topic/31404/a-short-o-n-recursive-greedy-solution
*/
public String removeDuplicateLetters(String s) {
int[] count = new int[26];
int pos = 0; // the position for the smallest s[i]
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
}
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) < s.charAt(pos)) {
pos = i;
}
count[s.charAt(i) - 'a']--;
if (count[s.charAt(i) - 'a'] == 0) {
break;
}
}
return s.length() == 0 ? "" : s.charAt(pos) + removeDuplicateLetters(
s.substring(pos + 1).replaceAll("" + s.charAt(pos), ""));
}
}
}