Problem overview
The method phoneBookLookup receives phoneBook, an object used as a dictionary from names to phone numbers, and a name. It returns the phone number stored under that name. The name is always a key of the phone book.
With {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} and name = "Ben", the answer is "555-0199". Two people can share a number: with {"Tom": "555-0111", "Tim": "555-0111"} and "Tim", the answer is "555-0111". Only the value under the requested key matters.
Approach
In TypeScript a plain object holds key and value pairs, so it works as a dictionary. The type { [key: string]: string } says that every key is a string and every value is a string. To read the value stored under a key, put the key in square brackets after the object.
The key here is not written in the code. It sits in the variable name, and the number to return depends on what that variable holds. Square brackets evaluate the expression inside them, so phoneBook[name] looks up the key whose text is the current value of name.
Step by step
- Read the value with brackets and return it. One line does the whole job:
phoneBook[name]is the number, andreturnhands it back.
The solution in TypeScript
class Solution {
phoneBookLookup(phoneBook: { [key: string]: string }, name: string): string {
return phoneBook[name];
}
}
The detail that matters is the difference between brackets and the dot:
- Brackets use the variable's value. With
name = "Ben",phoneBook[name]reads the key"Ben". - The dot uses a literal key.
phoneBook.namealways looks for a key spelledname. With{"Ana": "555-0142"}it givesundefined, because there is no such key, and the variablenameis never consulted. - A missing key gives
undefined. ReadingphoneBook["Zed"]from that same object also givesundefined, with no error. This lesson promises that the name is always present, so the code doesn't need to handle that case. - Types are erased. The
stringreturn type is a promise to the compiler. At run time nothing checks that the value is a string.
Complexity
Time: O(1) on average. Reading a property from an object is a hash-style lookup, and it does not scan the entries one by one.
Space: O(1). The method creates nothing new. It returns a value that is already stored in the object.
Edge cases and common mistakes
- Single entry.
{"Max": "555-0100"}with"Max"returns"555-0100". - Shared numbers. Different keys may hold the same value, as with Tom and Tim. The lookup goes from the key to the value, so this causes no trouble.
- Dot notation.
phoneBook.namecompiles without any error for this parameter type, but it returnsundefinedunless the object has a key spelledname. Use brackets. - Quotes around the variable.
phoneBook["name"]is the dot version again: it asks for the literal keyname. - Numbers are text. The values are strings such as
"555-0142", with a dash, so the method returns them unchanged and does no arithmetic on them. - Case. Keys are compared exactly, so
"ben"is not"Ben".
Testing
{"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}with"Ben"gives"555-0199".{"Max": "555-0100"}with"Max"gives"555-0100".{"Tom": "555-0111", "Tim": "555-0111"}with"Tim"gives"555-0111".
