Contacts

  • + 0 comments

    The following simple code worked for me. Since the length of name is less than 22, both time complexity and space complexity are O(n).

    from collections import defaultdict
    def contacts(queries):
        # Write your code here
        record = defaultdict(int)
        ans = []
        for operation, name in queries:
            if operation == 'add':
                for i in range(len(name)):
                    record[name[:i+1]] += 1
            else:
                ans.append(record[name])
        return ans