Skip to content

Latest commit

 

History

History
86 lines (64 loc) · 4.06 KB

File metadata and controls

86 lines (64 loc) · 4.06 KB

791. Custom Sort String (Medium)

Date and Time: Dec 4, 2024, 23:13 (EST)

Link: https://leetcode.com/problems/custom-sort-string


Question:

You are given two strings order and s. All the characters 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.


Example 1:

Input: order = "cba", s = "abcd"

Output: "cbad"

Explanation: "a", "b", "c" appear in order, so the order of "a", "b", "c" should be "c", "b", and "a".

Since "d" does not appear in order, it can be at any position in the returned string. "dcba", "cdba", "cbda" are also valid outputs.

Example 2:

Input: order = "bcafg", s = "abcd"

Output: "bcad"

Explanation: The characters "b", "c", and "a" from order dictate the order for the characters in s. The character "d" in s does not appear in order, so its position is flexible.

Following the order of appearance in order, "b", "c", and "a" from s should be arranged as "b", "c", "a". "d" can be placed at any position since it's not in order. The output "bcad" correctly follows this rule. Other arrangements like "dbca" or "bcda" would also be valid, as long as "b", "c", "a" maintain their order.


Constraints:

  • 1 <= order.length <= 26

  • 1 <= s.length <= 200

  • order and s consist of lowercase English letters.

  • All the characters of order are unique.


Walk-through:

  1. Loop over s to save each char with their counts into hashmap{}.

  2. Loop over order and check if char exists in hashmap{}, if so, add this char c with repeated times hashmap[c] to res (res += c * hashmap[c]). Delete this key-value pair in hashmap{}.

  3. Loop over hashmap{} to add all the missing chars with their repeated times to res.


Python Solution:

class Solution:
    def customSortString(self, order: str, s: str) -> str:
        # Loop over s and save each char with counts
        # Loop over `order`, if char exists in hashmap, we add this char * counts to res. Delete this key-val entry in hashmap
        # Loop over hashmap to add the missing chars into res
        
        # TC: O(n + k), n = len(s), k = len(order), SC: O(n)
        hashmap = {}
        res = ""
        # Calculate each char in s with their frequency
        for c in s:
            hashmap[c] = hashmap.get(c, 0) + 1
        # Follow the order to append chars into res
        for c in order:
            if c in hashmap:
                res += c * hashmap[c]
                del hashmap[c]
        # Check the missing chars
        for key, val in hashmap.items():
            res += key * val
        return res

Time Complexity: $O(n)$
Space Complexity: $O(n)$, length of hashmap and res is only related to s


CC BY-NC-SABY: credit must be given to the creatorNC: Only noncommercial uses of the work are permittedSA: Adaptations must be shared under the same terms