We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 8: Dictionaries and Maps
Day 8: Dictionaries and Maps
Sort by
recency
|
2657 Discussions
|
Please Login in order to post a comment
here is the code:-
this my Cpp solution
int main() { map< string, int> phonebook; int T; cin >> T; string name, findname; int number;
}
Let's analyze the time and space complexity of the provided code:
Time Complexity: - Inserting entries into the phone book: This takes O(n) time, where n is the number of entries. - Processing queries: In the worst case, we might have to process all queries, which would take O(q) time, where q is the number of queries. - Overall, the time complexity of the code is O(n + q).
Space Complexity: - Storing the phone book entries: This requires O(n) space to store n entries. - Storing the phone book dictionary: This requires O(n) space to store the dictionary. - Other variables: The space required for other variables like 'entry', 'name', 'phone_number', and 'query' is constant, hence negligible. - Overall, the space complexity of the code is O(n).
In summary: - Time complexity: O(n + q) - Space complexity: O(n)name__ == "main": phonebook()
Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input()) phonebook = {}
for i in range(n): name, phone = input().split() phonebook.update({name: phone})
while True: try: query = input() if query in phonebook: print(query +'='+ phonebook[query]) else: print('Not found') except EOFError: break
PHP