-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1081.java
44 lines (40 loc) · 1.89 KB
/
_1081.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
package com.fishercoder.solutions;
import java.util.*;
// same as 316. Remove Duplicate Letters
// from https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_316.java
public class _1002 {
public class Solution1 {
public String smallestSubsequence(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();
}
}
}