• + 0 comments

    This is my solution for Python 3.5+ using dictionaries.

    def contacts(queries):
        arr = defaultdict(list)
        r = []
        
        def find(word):
            index = word[0]
            l = len(word)
            r = 0
            
            for v in arr[index]:
                if v[:l] == word:
                    r +=1
            return r
            
        for q in queries:
            op, v = q
            
            if op == 'add':
                arr[v[0]].append(v)
            elif op == 'find':
                r.append(find(v))
        return r