Resolve Key-Value Chains
Problem statement
You are given an array mappings. Each element is a string key:value.
A value may be the name of another key. Then the key's final value is the final value of that other key. A value that is not any key is final as it is.
Return an array in the same order as mappings, where element i is key:final for the key of mappings[i] and its final value.
Function
resolveMappings(mappings: String[]) → String[]Examples
Example 1
mappings = ["a:1","b:2","c:a"]return = ["a:1","b:2","c:1"]1 and 2 are not keys, so a and b keep them. c maps to the key a, whose final value is 1, so c becomes c:1.
Example 2
mappings = ["x:y","y:z","z:42","w:q"]return = ["x:42","y:42","z:42","w:q"]x goes to y, then z, then 42, which is not a key. y and z also end at 42. q is not a key, so w keeps it.
Example 3
mappings = ["p7:3","3:end","k:p7"]return = ["p7:end","3:end","k:end"]The key 3 is made of a digit, but it is still a key. p7 goes to 3 and then to end, and k goes to p7 and ends at end too.
Constraints
1 <= mappings.length <= 1000- Each
mappings[i]has the formkey:valuewith exactly one colon, wherekeyandvalueare each 1 to 8 lowercase English letters or digits. - All keys are different.
- Following values from any key never comes back to a key already passed, so there are no loops.