Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Custom Sort String

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "791" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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, 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.

Explanation

Here's a solution that prioritizes efficiency and optimality:

  • Character Counting: Create a frequency map (dictionary or array) to count occurrences of each character in s.
  • Ordered Construction: Iterate through order, appending characters to the result string based on their counts from the frequency map. Decrement the counts as characters are added.
  • Remaining Characters: Iterate through the frequency map. Add any remaining characters (those not present in order) to the result string.

  • Runtime Complexity: O(N + M), where N is the length of order and M is the length of s.

  • Storage Complexity: O(1), The size of the frequency map is bounded by the size of the alphabet (26).

Code

    def customSortString(order: str, s: str) -> str:
    """
    Permutes the characters of s to match the order specified by the string order.

    Args:
        order: A string representing the custom order of characters.
        s: A string to be permuted according to the custom order.

    Returns:
        A permutation of s that satisfies the order specified by order.
    """

    char_counts = {}
    for char in s:
        char_counts[char] = char_counts.get(char, 0) + 1

    result = ""
    for char in order:
        if char in char_counts:
            result += char * char_counts[char]
            del char_counts[char]

    for char, count in char_counts.items():
        result += char * count

    return result

More from this blog

C

Chatmagic blog

2894 posts