Problem overview
Version 3 of the cat project adds a function, showAllCat(lines: string[]): string, that makes whitespace visible. Every tab character is replaced by the two characters ^I, and every line ends with a $ marker, so trailing spaces show up. The lines are then joined with newlines.
This version handles tabs and line ends only. The real cat -A also shows other control characters, but those are outside the lesson. A blank line becomes just $, and an empty list gives an empty string.
For ["hello\tworld", "foo"] the result is "hello^Iworld$\nfoo$". For ["a\t", "", "x "] it is "a^I$\n$\nx $", which prints as:
a^I$
$
x $
Approach: two changes per line, then join
Each line gets the same two changes, in this order: replace every tab with ^I, then append $. After that, the changed lines are joined with "\n", as in the earlier versions.
The only tricky part is "every tab". The string method replace with a string pattern changes only the first match. For "a\tb\tc", replace("\t", "^I") gives "a^Ib\tc", and the second tab stays. The lesson avoids that with split and join: split the line at every tab into pieces, then glue the pieces back together with ^I between them. That produces "a^Ib^Ic".
Step by step
- Create an empty array
outof typestring[]for the changed lines. - Loop over the positions with
ifrom 0 whilei < lines.length, so thatlines[i]is the current line. - Push the changed line: split it at the tab characters, join the pieces with
^I, and add$at the end. The marker goes last, after the tabs are handled. - After the loop, return
out.join("\n"), with newlines between rows and none after the last one.
The solution in TypeScript
class Solution {
showAllCat(lines: string[]): string {
let out: string[] = [];
for (let i = 0; i < lines.length; i++) {
out.push(lines[i].split("\t").join("^I") + "$");
}
return out.join("\n");
}
}
split("\t")returns an array of the pieces between tabs. A line with no tab gives a one-element array, andjointhen returns the line unchanged. A line that starts or ends with a tab gives an empty piece at that end, which becomes a leading or trailing^Iafter the join.- Two tabs in a row give an empty piece between them:
"a\t\tb"becomes"a^I^Ib". - Modern JavaScript also has
replaceAll("\t", "^I"), which gives the same result. The split-and-join form works in any version. - The
+ "$"applies to the whole joined line, because the method calls bind tighter than+.
Complexity
Each character of each line is visited a constant number of times, so time and space are both O(total length).
Edge cases and common mistakes
- Using
replacewith a string. It handles only the first tab. A line with two tabs would come out half converted. - Blank lines.
""has no tabs and becomes"$", so a blank line stays visible. - Trailing spaces. They are kept, and the
$follows them:"x "becomes"x $". That is the point of the marker. - Tab at the end.
"a\t"becomes"a^I$". - Empty list. Nothing is pushed, and the result is
"", not"$". - The marker is added per line, not once at the end of the whole output.
Testing
| Input | Result |
|---|---|
["hello\tworld", "foo"] | "hello^Iworld$\nfoo$" |
["a\t", "", "x "] | "a^I$\n$\nx $" |
["a\t\tb"] | "a^I^Ib$" |
[] | "" |
