Problem overview
The second version of the tr tool translates a whole set of characters at once. The method tr receives a list of lines and two strings of equal length, set1 and set2. In every line, a character found at index i of set1 is replaced by set2[i]; other characters are unchanged. The lines are returned joined by "\n".
For ["banana", "cherry"] with set1 = "abc" and set2 = "ABC", the result is "BAnAnA\nCherry". The characters are literal, with no ranges such as a-z, the sets have equal length, and set1 has no repeated character. Lines may be empty.
One rule shapes the solution: all replacements happen at once. With set1 = "ab" and set2 = "ba", the two letters swap places.
Approach: build a table once, translate each line with it
The previous version used replace for one character. Chaining replace calls for each pair of the sets fails when replacements overlap: "banana".replace("a", "b").replace("b", "a") gives "aanana", because the second call also changes the bs the first call just made. A swap needs a different tool.
Python's string method translate does the whole job in one pass over the line. It takes a translation table, and str.maketrans(set1, set2) builds that table by pairing the characters of the two strings by position. For "abc" and "ABC" the table is {97: 65, 98: 66, 99: 67}, a dictionary from character codes to character codes. Characters that are not in the table pass through unchanged. With the table maketrans("ab", "ba"), "banana".translate(table) gives "abnbnb": every letter is looked up independently, so nothing is translated twice.
Step by step
- Build
table = str.maketrans(set1, set2). The first character ofset1pairs with the first ofset2, and so on. Build it once, before the loop. - Create an empty list
result. - Write a
forloop withlinetaking each whole line oflines. - Inside the loop, append
line.translate(table)toresult. - After the loop, return
"\n".join(result).
The solution in Python
class Solution:
def tr(self, lines, set1, set2):
table = str.maketrans(set1, set2)
result = []
for line in lines:
result.append(line.translate(table))
return "\n".join(result)
Details to remember:
maketransis called on the class,str.maketrans, and does not depend on any particular string.- The two strings must have the same length. Otherwise Python raises
ValueError: the first two maketrans arguments must have equal length. The lesson guarantees equal lengths, so no check is needed. - Building the table once is cheaper than rebuilding it for each line, and keeps the loop body to one line.
- As with every string method,
translatereturns a new string. The original line is unchanged.
Complexity
Building the table costs time proportional to the length of the sets. Translating a line costs one dictionary lookup per character, so the total time is linear in the number of characters in all lines, plus the size of the sets. The table and the output need extra space of the same order.
Edge cases and common mistakes
| lines | set1 | set2 | result |
|---|---|---|---|
["banana", "cherry"] | "abc" | "ABC" | "BAnAnA\nCherry" |
["banana", "ab"] | "ab" | "ba" | "abnbnb\nba" |
["hello"] | "" | "" | "hello" |
- The second row is the swap: every
abecomesband everybbecomesa, in the same pass. - Chaining
replacecalls, one per pair, breaks the swap, as the"aanana"example above shows. - Case matters: with
set1 = "abc", an uppercase"C"is not translated. - Empty lines translate to empty lines and still occupy their place in the output.
