Problem overview
This version builds the -v flag of grep, which inverts the match. grepInvert(lines: string[], pattern: string): string returns the lines that do not contain pattern as a substring, in their original order, joined by newline characters. The match is case-sensitive. If every line contains the pattern, the result is "".
With pattern = "err" and lines = ["error", "ok", "another error", "fine"], the lines "error" and "another error" contain err and are dropped. The result is "ok\nfine".
Approach: keep the lines where the test is false
The previous version kept a line when includes(pattern) was true. Inversion needs no new machinery: keep a line when that test is false. In code that is one character, a ! in front of the call. The loop, the result array and the final join stay the same.
It helps to read the condition aloud: "if the line does not include the pattern, keep it". Everything else, including the order of the lines, follows from the loop visiting them from first to last.
Step by step
- Create the result: an empty
string[]namedresult. The name now holds the lines that were kept, which here are the non-matching ones. - Loop by position with
ifrom 0 whilei < lines.length. - Test for absence.
if (!lines[i].includes(pattern))is true when the pattern appears nowhere in the line. The!flips true into false and false into true. - Keep the line.
result.push(lines[i])inside theif. A line containing the pattern never reaches it. - Join and return with
result.join("\n"). If every line was dropped,resultis empty and joining it gives"".
The solution in TypeScript
class Solution {
grepInvert(lines: string[], pattern: string): string {
let result: string[] = [];
for (let i = 0; i < lines.length; i++) {
if (!lines[i].includes(pattern)) {
result.push(lines[i]);
}
}
return result.join("\n");
}
}
Language details:
!is the logical NOT operator. It applies to the whole call that follows it,lines[i].includes(pattern), which returns aboolean. No extra parentheses are needed, because a method call binds tighter than!.includeson a string is case-sensitive and compares plain text, so a line such as"Error"does not contain"err"and is therefore kept.- An easy alternative is
lines.filter(line => !line.includes(pattern)).join("\n"). It does the same thing in one expression, and the loop in the lesson shows each step separately. - The method returns a
stringin every case, including when nothing is kept.[].join("\n")is"", notnullorundefined.
Complexity
Each of the m lines is searched once, so the time is proportional to the total size of the lines. The result array can hold up to all of them, so the extra space is up to the size of the input.
Edge cases and common mistakes
- Forgetting the
!. Without it the method is the plaingrepand returns the matching lines. For the example it would return"error\nanother error". - Everything matches.
["error"]with"err"gives"". - Empty list. An empty array gives
""as well. - Order. The kept lines stay in their original order, because the loop goes from the first to the last and
pushappends. - Case. The test is case-sensitive, so with the pattern
"err"a line like"Error"is not dropped.
Testing
| lines | pattern | Result |
|---|---|---|
["error", "ok", "another error", "fine"] | "err" | "ok\nfine" |
["error"] | "err" | "" |
[] | "err" | "" |
The next version builds on this one.
