Advertisement

Custom Sort String - LeetCode 791 Solution

Custom Sort String - Complete Solution Guide

Custom Sort String is LeetCode problem 791, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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,

Detailed Explanation

The problem asks us to rearrange the characters of string `s` according to the order specified by string `order`. The string `order` contains unique characters, defining a custom order. We need to permute `s` such that if a character `x` appears before `y` in `order`, then `x` must also appear before `y` in the rearranged `s`. Characters in `s` that are *not* present in `order` can be placed anywhere in the result. The goal is to return *any* valid permutation of `s` adhering to this rule.

Solution Approach

The solution uses a counting approach. First, it counts the occurrences of each character in string `s` using a hash map or a counter (depending on the language). Then, it iterates through the characters in `order`. For each character in `order`, if it exists in the character counts of `s`, it appends that character to the result string as many times as it appears in `s`, updating the count accordingly (or removing it from the count). Finally, it iterates through the remaining characters in `s` (those not in `order`) and appends them to the result string based on their counts.

Step-by-Step Algorithm

  1. Step 1: Create a hash table `s_count` to store the character counts from the string `s`.
  2. Step 2: Iterate through the string `s` and populate `s_count` with each character's frequency.
  3. Step 3: Initialize an empty string `res` to store the result.
  4. Step 4: Iterate through the string `order`. For each character `char` in `order`:
  5. Step 5: Check if `char` exists as a key in `s_count`.
  6. Step 6: If `char` exists in `s_count`, append `char` to `res` `s_count[char]` times.
  7. Step 7: Remove `char` from `s_count` to avoid processing it again.
  8. Step 8: Iterate through the remaining entries in `s_count`. For each character `char` and its count in `s_count`:
  9. Step 9: Append `char` to `res` the number of times specified by its count.
  10. Step 10: Return the resulting string `res`.

Key Insights

  • Insight 1: The key is to respect the custom order specified in the `order` string. We can iterate through `order` and extract/append the respective characters from `s` based on their counts.
  • Insight 2: A hash table (or similar data structure like a counter) is useful to efficiently count the occurrences of each character in `s`.
  • Insight 3: After processing characters present in `order`, we need to append the remaining characters from `s` (those not present in `order`) to the result. The order of these remaining characters doesn't matter.

Complexity Analysis

Time Complexity: O(n + m)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String, Sorting.

Companies

Asked at: Snap.