This repository was archived by the owner on Sep 20, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +26
-31
lines changed
Algorithms/1081.smallest-subsequence-of-distinct-characters Expand file tree Collapse file tree 1 file changed +26
-31
lines changed Original file line number Diff line number Diff line change 1
1
package problem1081
2
2
3
- import (
4
- "sort"
5
- "strings"
6
- )
3
+ import "strings"
7
4
8
5
func smallestSubsequence (text string ) string {
9
6
rec := make ([][]int , 26 )
7
+ count := 0
10
8
for i , b := range text {
11
9
c := b - 'a'
12
10
rec [c ] = append (rec [c ], i )
11
+ if len (rec [c ]) == 1 {
12
+ count ++
13
+ }
13
14
}
14
15
15
- letters := make ([]* letter , 0 , 26 )
16
- for i := 0 ; i < 26 ; i ++ {
17
- if len (rec [i ]) == 0 {
18
- continue
16
+ flag := - 1
17
+ beforeAll := func () bool {
18
+ ok := true
19
+ for i := 0 ; i < 26 && ok ; i ++ {
20
+ if len (rec [i ]) == 0 {
21
+ continue
22
+ }
23
+
19
24
}
20
- letters = append (letters , & letter {
21
- char : byte (i + 'a' ),
22
- indexs : rec [i ],
23
- })
25
+ return ok
24
26
}
25
27
26
- sort .Slice (letters , func (i int , j int ) bool {
27
- return isLess (letters [i ], letters [j ])
28
- })
29
-
30
28
var sb strings.Builder
31
- for _ , l := range letters {
32
- sb .WriteByte (l .char )
29
+ for i := 0 ; i < count ; i ++ {
30
+ for j := 0 ; j < 26 ; j ++ {
31
+ if len (rec [i ]) == 0 {
32
+ continue
33
+ }
34
+ j := 0
35
+ for j < len (rec [i ]) && rec [i ][j ] < flag {
36
+ j ++
37
+ }
38
+ index := rec [i ][0 ]
39
+ }
40
+
33
41
}
34
42
35
43
return sb .String ()
36
44
}
37
-
38
- type letter struct {
39
- char byte
40
- indexs []int
41
- }
42
-
43
- func isLess (a , b * letter ) bool {
44
- if a .indexs [0 ] < b .indexs [len (b .indexs )- 1 ] &&
45
- b .indexs [0 ] < a .indexs [len (a .indexs )- 1 ] {
46
- return a .char < b .char
47
- }
48
- return false
49
- }
You can’t perform that action at this time.
0 commit comments