Problem overview
The method invertDictionary receives an object d with string keys and string values. It returns a new object with keys and values swapped: every value of d becomes a key, mapped to the key it belonged to. The values of d are all different, so the result has as many entries as d.
For {"apple": "red", "banana": "yellow", "plum": "purple"} the result is {"red": "apple", "yellow": "banana", "purple": "plum"}. For {"cat": "dog"} it is {"dog": "cat"}.
The third example is the interesting one: {"a": "b", "b": "c"} gives {"b": "a", "c": "b"}. The text "b" is a value in one entry and a key in another. That is fine, because the two roles are separate: the value "b" of the entry "a": "b" becomes a key mapping to "a", and the entry "b": "c" turns into "c": "b".
Approach
Build the answer in a new object and leave d alone. One pass over the entries of d is enough, since each entry turns around on its own. For each pair, store result[value] = key.
To read the pairs you need both the key and the value of each entry. Object.entries(d) returns them as an array of two-item arrays, such as [["apple", "red"], ["banana", "yellow"]].
On {"cat": "dog", "owl": "bird"}, Object.entries gives [["cat", "dog"], ["owl", "bird"]]. The first round stores result["dog"] = "cat" and the second stores result["bird"] = "owl", so the answer is {"dog": "cat", "bird": "owl"}. The guarantee that the values differ is what makes this safe: no two entries write to the same new key, so none is lost.
Step by step
- Create the empty object.
resultis declared withconstand the same type asd,{ [key: string]: string }. - Loop over the entries.
for (const [key, value] of Object.entries(d))unpacks each two-item array into the variableskeyandvalue. - Store the swapped pair.
result[value] = keymakes the old value the new key. - Return
resultafter the loop.
The solution in TypeScript
class Solution {
invertDictionary(d: { [key: string]: string }): { [key: string]: string } {
const result: { [key: string]: string } = {};
for (const [key, value] of Object.entries(d)) {
result[value] = key;
}
return result;
}
}
Details worth knowing:
- Destructuring.
const [key, value]takes the first item of the pair askeyand the second asvalue. It is shorthand for readingentry[0]andentry[1]. Object.entriesversusfor...in. Afor...inloop gives only the keys, so you would look each value up withd[key].Object.entrieshands you both at once.- The input is untouched. The loop only reads
dand writes toresult, so adding toresultwhile looping is safe. - Keys are always strings. That suits this lesson, whose values are strings, so they work as keys directly.
Inverting twice gives back the original object, as long as the values were all different. That makes a quick sanity check for your own tests: invertDictionary(invertDictionary(d)) should have the same entries as d.
Edge cases and common mistakes
- Repeated values. The lesson guarantees all values differ. If two did not, they would land on the same key and the later entry would win:
{"a": "x", "b": "x"}gives{"x": "b"}, and "a" is lost. - Swapping inside
d. Changingdwhile reading it is confusing and is not asked for. The method returns a new object. - Writing
result[key] = value. That just copiesd. - A word that is both key and value. The case
{"a": "b", "b": "c"}works without special handling, because the result is a separate object.
Testing
{"apple": "red", "banana": "yellow", "plum": "purple"}gives{"red":"apple","yellow":"banana","purple":"plum"}.{"cat": "dog"}gives{"dog":"cat"}.{"a": "b", "b": "c"}gives{"b":"a","c":"b"}.
