파비의 매일매일 공부기록

2023.02.09 Today's Challenge 본문

Problem Solving/LeetCode

2023.02.09 Today's Challenge

fabichoi 2023. 2. 9. 23:45

https://leetcode.com/problems/naming-a-company/

 

Naming a Company - LeetCode

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

leetcode.com

문자열을 조합하는 문제

class Solution:
    def distinctNames(self, ideas: List[str]) -> int:
        ans = 0
        suffixes = [set() for _ in range(26)]

        for idea in ideas:
            suffixes[ord(idea[0]) - ord('a')].add(idea[1:])
        
        for i in range(25):
            for j in range(i+1, 26):
                count = len(suffixes[i] & suffixes[j])
                ans += 2 * (len(suffixes[i]) - count) * (len(suffixes[j]) - count)
        
        return ans
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.02.11 Today's Challenge  (0) 2023.02.11
2023.02.10 Today's Challenge  (0) 2023.02.10
2023.02.08 Today's Challenge  (0) 2023.02.08
2023.02.07 Today's Challenge  (0) 2023.02.07
2023.02.06 Today's Challenge  (0) 2023.02.06
Comments