Tries: Contacts

  • + 3 comments

    Python 3 - bit of a hack but it works.

    import bisect
    n = int(input().strip())
    contacts = []
    for a0 in range(n):
        op, contact = input().strip().split(' ')
    
        if op == 'add':
            bisect.insort_left(contacts, contact)
    
        if op == 'find':
            left = bisect.bisect_left(contacts, contact)
            right = bisect.bisect_left(contacts, contact + 'zzzzzzz', left)
            print(right - left)