Created
May 28, 2020 14:26
-
-
Save panki/7f2c4ae548d635eb4181b1b7fbd437f4 to your computer and use it in GitHub Desktop.
1370. Increasing Decreasing String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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