mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 23:04:42 +01:00
20210104
This commit is contained in:
@@ -1,9 +1,40 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func solution(s string) string {
|
func solution(s string) string {
|
||||||
return ""
|
var stack [26]int
|
||||||
|
max := 0
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
index := s[i] - 'a'
|
||||||
|
stack[index]++
|
||||||
|
if stack[index] > max {
|
||||||
|
max = stack[index]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb := strings.Builder{}
|
||||||
|
up := true
|
||||||
|
for i := 0; i < max; i++ {
|
||||||
|
if up {
|
||||||
|
for j := 0; j < len(stack); j++ {
|
||||||
|
if stack[j] > 0 {
|
||||||
|
sb.WriteByte(uint8(j) + 'a')
|
||||||
|
stack[j]--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for j := len(stack) - 1; j >= 0; j-- {
|
||||||
|
if stack[j] > 0 {
|
||||||
|
sb.WriteByte(uint8(j) + 'a')
|
||||||
|
stack[j]--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
up = !up
|
||||||
|
}
|
||||||
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,34 @@ func Test_solution(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"abccbaabccba",
|
"abccbaabccba",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"test2",
|
||||||
|
args{
|
||||||
|
"rat",
|
||||||
|
},
|
||||||
|
"art",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test3",
|
||||||
|
args{
|
||||||
|
"leetcode",
|
||||||
|
},
|
||||||
|
"cdelotee",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test4",
|
||||||
|
args{
|
||||||
|
"ggggggg",
|
||||||
|
},
|
||||||
|
"ggggggg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test5",
|
||||||
|
args{
|
||||||
|
"spo",
|
||||||
|
},
|
||||||
|
"ops",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user