Similar Problems

Similar Problems not available

Naming A Company - Leetcode Solution

Companies:

LeetCode:  Naming A Company Leetcode Solution

Difficulty: Hard

Topics: string hash-table bit-manipulation array  

Problem Statement:

You are a part of a team starting a new company and it's your job to come up with a name. The name must be unique and follow these rules:

  1. The name must start with a letter.
  2. The name must contain only lowercase letters or underscores.
  3. It must not contain two consecutive underscores.
  4. The name must be no more than 20 characters long.

Write a function that takes in a list of existing company names and returns a new name that meets the above criteria.

Function Signature:

The function signature is:

def naming_company(existing_names: List[str]) -> str:

where,

  • existing_names is a list of strings representing the names of the companies that already exist.
  • The function returns a string which is a new company name that meets the given criteria.

Examples:

Example 1:

existing_names = ["company", "company_1", "company_2"] output = naming_company(existing_names) print(output)

Output: "a"

Example 2:

existing_names = ["company", "company_1", "company_2", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] output = naming_company(existing_names) print(output)

Output: "aa"

Solution:

The solution to this problem involves generating a new name that meets the criteria mentioned in the problem statement. We can use a brute-force approach to generate new names.

We start by generating a single-letter name, then continue to generate two-letter names, and so on until we find a name that is not already in the list of existing names. We can use a while loop to keep generating new names until we find one that meets the given criteria.

Here is the Python code to implement the solution:

from typing import List

def naming_company(existing_names: List[str]) -> str:
    i = 1
    
    while True:
        new_name = chr(ord("a") + (i-1) % 26) * ((i-1) // 26 + 1)
        if "__" not in new_name and new_name not in existing_names and len(new_name) <= 20:
            return new_name
        i += 1

In the above code, we start by initializing i to 1, which represents the length of the name we are generating. We then enter a while loop that generates new names until we find one that meets the given criteria.

We generate the new name using the formula chr(ord("a") + (i-1) % 26) * ((i-1) // 26 + 1). This formula generates the characters of the name one by one, starting with "a" and looping back to "a" after every 26 characters. The expression (i-1) // 26 + 1 calculates the number of blocks of 26 characters needed to generate a name of length i.

We then check if the new name meets the criteria by checking if it contains two consecutive underscores, if it is already in the list of existing names, and if its length is less than or equal to 20. If the name meets the criteria, we return it.

If the name does not meet the criteria, we increment i and continue generating new names until we find one that meets the criteria.

The time complexity of this algorithm is O(n^2), where n is the length of the longest existing name in the list. This is because we generate new names of increasing length until we find one that meets the criteria, and the length of the longest existing name determines the upper bound of the length of the new name we generate. However, in practice, the algorithm is likely to terminate much sooner, since we are unlikely to need to generate names longer than a few characters.

Naming A Company Solution Code

1