You are viewing a single comment's thread. Return to all comments →
#!/bin/python3 import math import os import random import re import sys class Node: def __init__(self, val): self.val = val self.next = [] self.count = 1 @staticmethod def add(root, contact): for i in contact: found = False for child in root.next: if child.val == i: child.count += 1 root = child found = True break if not found: char = Node(i) root.next.append(char) root = char @staticmethod def find(root, contact): count = 0 for i in contact: found = False for child in root.next: if i == child.val: count = child.count root = child found = True break if not found: count = 0 break print(count) if __name__ == '__main__': n = int(input().strip()) root = Node(0) for n_itr in range(n): first_multiple_input = input().rstrip().split() op = first_multiple_input[0] contact = first_multiple_input[1] if op == 'add': Node.add(root, contact) if op == 'find': Node.find(root, contact)
Seems like cookies are disabled on this browser, please enable them to open this website
Tries: Contacts
You are viewing a single comment's thread. Return to all comments →