forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomSortString.kt
37 lines (33 loc) · 1.18 KB
/
CustomSortString.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* You are given two strings order and s. All the words of order are unique
* and were sorted in some custom order previously.
* Permute the characters of s so that they match the order that order was sorted.
* More specifically, if a character x occurs before a character y in order,
* then x should occur before y in the permuted string.
*
* Return any permutation of s that satisfies this property.
*
* [Source](https://leetcode.com/problems/custom-sort-string-kt/)
*/
@UseCommentAsDocumentation
private fun customSortString(order: String, s: String): String {
val countMap: MutableMap<Char, Int> = s.groupingBy { it }.eachCount().toMutableMap()
val sb = StringBuilder()
for (i in order) {
if (countMap.contains(i)) {
sb.append(i.toString().repeat(countMap[i]!!))
countMap.remove(i)
}
}
countMap.forEach { (key, count) ->
sb.append(key.toString().repeat(count))
}
return sb.toString()
}
fun main() {
customSortString(order = "cba", s = "abcd") shouldBe "cbad"
customSortString(order = "cbafg", s = "abcd") shouldBe "cbad"
}