Custom Sort String LeetCode solution in Python
1# Runtime: 328 ms (Top 5.22%) | Memory: 13.9 MB (Top 17.87%)2class Solution:3def customSortString(self, order: str, s: str) -> str:4charValue = [0] * 265for i in range(len(order)):6idx = ord(order[i]) - ord("a")7charValue[idx] = 26 - i89arrS = []10n = 011for c in s:12arrS.append(c)13n += 11415sorted = False16while not sorted:17sorted = True18for i in range(n - 1):19if (20charValue[ord(arrS[i]) - ord("a")]21< charValue[ord(arrS[i + 1]) - ord("a")]22):23sorted = False24arrS[i], arrS[i + 1] = arrS[i + 1], arrS[i]2526return "".join(arrS)