Naming A Company

Solution For Naming A Company

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:


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


Example 1:

existing_names = [“company”, “company_1”, “company_2”] output = naming_company(existing_names)

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)

Output: “aa”


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.

Step by Step Implementation For Naming A Company

public class Solution {
    public String solution(String S) {
        // write your code in Java SE 8
        String[] parts = S.split("-");
        StringBuilder sb = new StringBuilder();
        for (String part : parts) {
        return sb.toString();
def naming_a_company(s): 

# create a list to store all the possible company names 

company_names = [] 

# iterate through the given string 

for i in range(len(s)): 

# iterate through the string starting from the current index 

for j in range(i, len(s)): 

# if the string from index i to j is not in the list 

if s[i:j + 1] not in company_names: 

# add the string to the list 

company_names.append(s[i:j + 1]) 

# return the list of company names 

return company_names
// Leetcode problem:

// Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.

// We repeatedly make duplicate removals on S until we no longer can.

// Return the final string after all such duplicate removals have been made.  It is guaranteed the answer is unique.


// Example 1:

// Input: "abbaca"
// Output: "ca"
// Explanation: 
// For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".

// Note:

// 1 <= S.length <= 20000
// S consists only of English lowercase letters.
// This problem was asked by Jane Street. 

// Suppose you are given a table of currency exchange rates, represented as a 2D array. Determine whether there is a possible arbitrage: that is, whether there is some sequence of trades you can make, starting with some amount A of any currency, so that you can end up with some amount greater than A of that currency. 

// There are no transaction costs and you can trade fractional quantities.


using namespace std;

bool hasArbitrage(vector>& rates) {
    int n = rates.size();
    vector cur(n, 1);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) continue;
            cur[j] = cur[j] * rates[i][j];
        if (cur[i] > 1) return true;
    return false;

int main() {
    vector> rates = {{1, 2, 1}, {2, 1, 2}};
    cout << hasArbitrage(rates) << endl;
    return 0;
namespace Company
    class Program
        static void Main(string[] args)
            Console.WriteLine("Enter a name for your company:");
            string companyName = Console.ReadLine();

            //Your code goes here

            Console.WriteLine("Your company name is: " + companyName);

Scroll to Top

Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]