본문 바로가기
LeetCode/Array & Hashing

[LeetCode][Kotlin] 2306. Naming a Company

by jinwo_o 2024. 10. 19.

2306. Naming a Company

You are given an array of strings ideas that represents a list of names to be used in the process of naming a company. The process of naming a company is as follows:

  1. Choose 2 distinct names from ideas, call them ideaA and ideaB.
  2. Swap the first letters of ideaA and ideaB with each other.
  3. If both of the new names are not found in the original ideas, then the name ideaA ideaB (the concatenation of ideaA and ideaB, separated by a space) is a valid company name.
  4. Otherwise, it is not a valid name.

Return the number of distinct valid names for the company.

회사 이름을 지정하는 과정에서 사용할 이름 목록을 나타내는 일련의 문자열 아이디어가 제공됩니다. 회사명을 정하는 과정은 다음과 같습니다. 
1. 아이디어에서 서로 다른 이름 2개를 선택하여 ideaA와 ideaB로 지정하세요. 
2. ideaA와 ideaB의 첫 글자를 서로 바꿉니다. 
3. 원래 아이디어에 새 이름이 모두 없으면 ideaA ideaB(공백으로 구분된 ideaA와 ideaB의 연결)라는 이름이 유효한 회사 이름입니다. 
4. 그렇지 않으면 유효한 이름이 아닙니다. 

회사의 유효한 고유 이름 수를 반환합니다.

 

Example 1:

Input: ideas = ["coffee","donuts","time","toffee"]

Output: 6

Explanation: The following selections are valid:

- ("coffee", "donuts"): The company name created is "doffee conuts".

- ("donuts", "coffee"): The company name created is "conuts doffee".

- ("donuts", "time"): The company name created is "tonuts dime".

- ("donuts", "toffee"): The company name created is "tonuts doffee".

- ("time", "donuts"): The company name created is "dime tonuts".

- ("toffee", "donuts"): The company name created is "doffee tonuts".

Therefore, there are a total of 6 distinct company names.

 

The following are some examples of invalid selections:

- ("coffee", "time"): The name "toffee" formed after swapping already exists in the original array.

- ("time", "toffee"): Both names are still the same after swapping and exist in the original array.

- ("coffee", "toffee"): Both names formed after swapping already exist in the original array.

 

Example 2:

Input: ideas = ["lack","back"]

Output: 0

Explanation: There are no valid selections. Therefore, 0 is returned.

 

Constraints:

  • 2 <= ideas.length <= 5 * 10^4
  • 1 <= ideas[i].length <= 10
  • ideas[i] consists of lowercase English letters.
  • All the strings in ideas are unique.

코드

class Solution {
    fun distinctNames(ideas: Array<String>): Long {
        val hm = HashMap<Char, HashSet<String>>()
        ideas.forEach { idea -> 
            if (idea[0] !in hm) {
                hm[idea[0]] = hashSetOf()
            }
            hm[idea[0]]!!.add(idea.substring(1, idea.length))
        }

        var answer = 0L
        for (i in hm.keys) {
            for (j in hm.keys) {
                if (i == j) continue

                // var intersect = 0
                // for (w in hm[i]!!) {
                //     if (w in hm[j]!!) {
                //         intersect++
                //     }
                // }
                val intersect = hm[i]!!.intersect(hm[j]!!).size
                val a = hm[i]!!.size - intersect
                val b = hm[j]!!.size - intersect
                answer += a * b
            }
        }

        return answer
    }
}