Skip to content

Instantly share code, notes, and snippets.

@panki
Created May 28, 2020 14:26
Show Gist options
  • Save panki/7f2c4ae548d635eb4181b1b7fbd437f4 to your computer and use it in GitHub Desktop.
Save panki/7f2c4ae548d635eb4181b1b7fbd437f4 to your computer and use it in GitHub Desktop.
1370. Increasing Decreasing String
// https://leetcode.com/problems/increasing-decreasing-string/
import "strings"
func sortString(s string) string {
counts := make([]int, 26)
for _, c := range s {
counts[int(c - 'a')] += 1
}
var res strings.Builder
for i, j, k := 0, 25, 1; res.Len() < len(s); i, j, k = j, i, -k {
for p := i; p != j + k; p += k {
if counts[p] == 0 {
continue
}
res.WriteString(string('a' + p))
counts[p] -= 1
}
}
return res.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment