Problem overview
The method Tr imitates a tiny tr command. It receives a List<string> of lines and two strings, fromChar and toChar, each holding one ASCII character. It replaces every occurrence of fromChar with toChar in every line and returns the lines joined with "\n". Upper and lower case count as different characters. A line may be empty, or may not contain fromChar at all.
For ["banana", "apple"] with fromChar = "a" and toChar = "X", the result is "bXnXnX\nXpple".
Approach: translate each line, then join
Each line is independent of the others, so the method handles one line at a time. It creates a list for the results, visits every line, adds the translated version and finally joins the list. The translation itself is a single call, because string has a Replace method that does the whole job.
Step by step
- Create
result, an emptyList<string>. It starts empty and gains one translated line per loop pass. - Write a
foreachloop that takes eachstring lineout oflines. No index is needed, since only the line itself matters. - Add
line.Replace(fromChar, toChar)toresult.Replacetakes the text to look for and the text to put in its place, and it changes every occurrence, not just the first. - After the loop, return
string.Join("\n", result). The separator goes between two lines only, so the output has no newline after the last line.
The solution in C#
class Solution {
public string Tr(List<string> lines, string fromChar, string toChar) {
List<string> result = new List<string>();
foreach (string line in lines) {
result.Add(line.Replace(fromChar, toChar));
}
return string.Join("\n", result);
}
}
The C# details:
- Strings are immutable.
Replacenever changesline. It returns a new string, and the loop stores that new string inresult. Writingline.Replace(fromChar, toChar);as a statement on its own would throw the result away. Replacehas astringversion and acharversion. Here the parameters arestrings, so theReplace(string, string)overload runs. Its text comparison is ordinal and case-sensitive, so"a"does not match"A".- An empty search text is an error.
"a".Replace("", "X")throws anArgumentException. The lesson saysfromCharis a single character, so that case does not occur. foreachcannot assign to its variable. Replacinglineinside the loop wouldn't compile, which is another reason to collect results in a second list.
Complexity
Let L be the total number of characters across all lines. Time: O(L), because each line is scanned once and rebuilt. Space: O(L) for the translated lines and the joined string.
Edge cases and common mistakes
- Empty line.
"".Replace("a", "X")is"", and the empty line stays in the output. For["a", "", "a"]with"a"and"X"the result is"X\n\nX". - No match. A line without
fromCharcomes back unchanged. - Case. With
fromChar = "a", the line"Apple"keeps its capitalA, so["banana", "Apple", ""]gives"bXnXnX\nApple\n". - Special characters. The match is literal.
["a.b.c"]with"."and"-"gives"a-b-c", and the dot is not a wildcard. - Ignoring the return value. Because strings are immutable,
Replacemust be assigned or added somewhere. - Joining with the wrong separator.
string.Jointakes the separator first, then the list.
Testing
["banana", "apple"],"a","X"gives"bXnXnX\nXpple".["a.b.c"],".","-"gives"a-b-c".["banana", "Apple", ""],"a","X"gives"bXnXnX\nApple\n".[],"a","X"gives"".
