Given a phone book phoneBook, a HashMap from names to phone numbers, and a name, return the phone number of name.
Every name in the phone book is a key, and its value is that person's phone number, a string like "555-0142". The name you look up is always in the phone book.
Example 1:
Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}, name = "Ben"
Output: "555-0199"
Explanation: The key "Ben" maps to "555-0199".
Example 2:
Input: phoneBook = {"Max": "555-0100"}, name = "Max"
Output: "555-0100"
Explanation: The phone book has a single entry, and it is the one we look up.
Example 3:
Input: phoneBook = {"Tom": "555-0111", "Tim": "555-0111"}, name = "Tim"
Output: "555-0111"
Explanation: Tom and Tim share a phone number. We only need the value of the key "Tim".
Constraints:
phoneBookhas at least one entry.nameis always a key ofphoneBook.- Names are single words made of English letters, and they are all different. Two people can share a phone number.
- Every phone number is a string of three digits, a dash and four digits, like
"555-0142".
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
