Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Making File Names Unique

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1487" 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

Given an array of strings names of size n. You will create n folders in your file system such that, at the ith minute, you will create a folder with the name names[i]. Since two files cannot have the same name, if you enter a folder name that was previously used, the system will have a suffix addition to its name in the form of (k), where, k is the smallest positive integer such that the obtained name remains unique. Return an array of strings of length n where ans[i] is the actual name the system will assign to the ith folder when you create it. Example 1: Input: names = ["pes","fifa","gta","pes(2019)"] Output: ["pes","fifa","gta","pes(2019)"] Explanation: Let's see how the file system creates folder names: "pes" --> not assigned before, remains "pes" "fifa" --> not assigned before, remains "fifa" "gta" --> not assigned before, remains "gta" "pes(2019)" --> not assigned before, remains "pes(2019)" Example 2: Input: names = ["gta","gta(1)","gta","avalon"] Output: ["gta","gta(1)","gta(2)","avalon"] Explanation: Let's see how the file system creates folder names: "gta" --> not assigned before, remains "gta" "gta(1)" --> not assigned before, remains "gta(1)" "gta" --> the name is reserved, system adds (k), since "gta(1)" is also reserved, systems put k = 2. it becomes "gta(2)" "avalon" --> not assigned before, remains "avalon" Example 3: Input: names = ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece"] Output: ["onepiece","onepiece(1)","onepiece(2)","onepiece(3)","onepiece(4)"] Explanation: When the last folder is created, the smallest positive valid k is 4, and it becomes "onepiece(4)". Constraints: 1 <= names.length <= 5 * 104 1 <= names[i].length <= 20 names[i] consists of lowercase English letters, digits, and/or round brackets.

Explanation

Here's the breakdown of the solution:

  • Use a hash map to track existing folder names: This allows for O(1) average time complexity lookups to check if a name has already been used.
  • Handle name collisions efficiently: When a collision occurs, generate new names by appending (k) until a unique name is found. Store the next available k for each base name to avoid re-checking existing suffixes.

  • Runtime & Space Complexity:

    • Runtime: O(n * m), where n is the number of names and m is the maximum number of iterations needed to find a unique name for a collision. In the worst case (highly repetitive names), m could approach n. On average, it's closer to O(n).
    • Space: O(n) to store the hash map of used names and the 'next available k' values for base names.

Code

    def getFolderNames(names):
    """
    Creates unique folder names based on the given input names.

    Args:
        names: A list of strings representing the desired folder names.

    Returns:
        A list of strings representing the actual folder names assigned by the system.
    """

    used_names = {}  # Store used folder names (name: True)
    name_counts = {}  # Store the next available count for each base name (name: next_count)
    result = []

    for name in names:
        if name not in used_names:
            used_names[name] = True
            result.append(name)
        else:
            base_name = name
            count = name_counts.get(base_name, 1)  # Start with 1 if base name not seen before

            while True:
                new_name = f"{base_name}({count})"
                if new_name not in used_names:
                    used_names[new_name] = True
                    result.append(new_name)
                    name_counts[base_name] = count + 1  # Update next count
                    break
                count += 1

    return result

More from this blog

C

Chatmagic blog

2894 posts