Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53f0c96

Browse files
author
Shuo
committed
A: Increasing Decreasing String
1 parent f7ea9e8 commit 53f0c96

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package problem1370
2+
3+
func sortString(s string) string {
4+
var m [26]int
5+
for _, c := range s {
6+
m[c-'a']++
7+
}
8+
l := len(s)
9+
ans := make([]byte, 0, l)
10+
appendChar := func(c int) {
11+
if m[c] > 0 {
12+
m[c]--
13+
l--
14+
ans = append(ans, byte(c+'a'))
15+
}
16+
}
17+
for l > 0 {
18+
for i := 0; i <= 25; i++ {
19+
appendChar(i)
20+
}
21+
for i := 25; i >= 0; i-- {
22+
appendChar(i)
23+
}
24+
}
25+
return string(ans)
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem1370
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in string
7+
want string
8+
}
9+
10+
func TestSortString(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: "aaaabbbbcccc",
14+
want: "abccbaabccba",
15+
},
16+
{
17+
in: "rat",
18+
want: "art",
19+
},
20+
{
21+
in: "leetcode",
22+
want: "cdelotee",
23+
},
24+
{
25+
in: "ggggggg",
26+
want: "ggggggg",
27+
},
28+
{
29+
in: "spo",
30+
want: "ops",
31+
},
32+
}
33+
for _, tt := range tests {
34+
got := sortString(tt.in)
35+
if got != tt.want {
36+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
37+
}
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /