-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRemoveDuplicateLetters316.kt
55 lines (39 loc) · 1.03 KB
/
RemoveDuplicateLetters316.kt
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
package medium
import java.lang.StringBuilder
import java.util.*
/**
* Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
Example 1:
Input: s = "bcabc"
Output: "abc"
Example 2:
Input: s = "cbacdcbc"
Output: "acdb"
Constraints:
1 <= s.length <= 104
s consists of lowercase English letters.
*/
fun removeDuplicateLetters(s: String): String {
val stack= Stack<Int>()
val lastIndex=IntArray(26)
val seen=BooleanArray(26)
for(i in s.indices)
lastIndex[s[i]-'a']=i
for (i in s.indices)
{
val c= s[i]-'a'
if(seen[c])continue
while (stack.isNotEmpty()&&stack.peek()>c&&i<lastIndex[stack.peek()])
{
seen[stack.pop()]=false
}
stack.push(c)
seen[c]=true
}
val result=StringBuilder()
while (stack.isNotEmpty())
{
result.insert(0,('a'.plus(stack.pop())))
}
return result.toString()
}