Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 6. 19. 23:45

https://leetcode.com/problems/search-suggestions-system/

 

Search Suggestions System - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

음.. 생각보다 너무 간단히 풀려서 놀람.
그냥 문제에서 원하는 대로 풀면 됨. TL이 날 줄 알았는데 무사통과!

class Solution:
    def suggestedProducts(self, products: List[str], searchWord: str) -> List[List[str]]:        
        products.sort()        
        s = ''
        results = []
        for search in searchWord:
            idx = 0
            s += search
            res = []            
            for p in products:
                if p[:len(s)] == s:                        
                    res.append(p)
                if len(res) >= 3:
                    break
            results.append(res)
        return results
반응형