Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 6a850c2

Browse files
committed
1081 accepted. 0ms
1 parent 8eb792f commit 6a850c2

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Algorithms/1081.smallest-subsequence-of-distinct-characters/smallest-subsequence-of-distinct-characters.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,44 @@ func smallestSubsequence(text string) string {
1313
}
1414
}
1515

16-
flag := -1
17-
beforeAll := func() bool {
16+
clean := func(flag int) {
17+
for i := 0; i < 26; i++ {
18+
if len(rec[i]) == 0 {
19+
continue
20+
}
21+
j := 0
22+
for j < len(rec[i]) && rec[i][j] < flag {
23+
j++
24+
}
25+
rec[i] = rec[i][j:]
26+
}
27+
}
28+
29+
beforeAll := func(index int) bool {
1830
ok := true
1931
for i := 0; i < 26 && ok; i++ {
2032
if len(rec[i]) == 0 {
2133
continue
2234
}
23-
35+
ok = index <= rec[i][len(rec[i])-1]
2436
}
2537
return ok
2638
}
2739

2840
var sb strings.Builder
2941
for i := 0; i < count; i++ {
3042
for j := 0; j < 26; j++ {
31-
if len(rec[i]) == 0 {
43+
if len(rec[j]) == 0 {
3244
continue
3345
}
34-
j := 0
35-
for j < len(rec[i]) && rec[i][j] < flag {
36-
j++
46+
index := rec[j][0]
47+
if beforeAll(index) {
48+
sb.WriteByte(byte(j + 'a'))
49+
rec[j] = nil
50+
clean(index)
51+
break
3752
}
38-
index := rec[i][0]
3953
}
40-
4154
}
4255

4356
return sb.String()

0 commit comments

Comments
 (0)