Build the -v flag of the Unix grep command. Given a list of lines and a search pattern, return all lines that DO NOT contain pattern as a substring (case-sensitive). Return the remaining lines (the ones that do not contain the pattern), in their original order, joined by newline characters (\n).
Function signature:
grepInvert(lines: string[], pattern: string): string
Example:
Input: pattern = "err" lines = ["error", "ok", "another error", "fine"] Output: "ok\nfine" When printed: ok fine
If every line contains the pattern, return the empty string "".
Same as
grep v1 but flip the condition: keep the lines where !lines[i].includes(pattern) is true, then return result.join("\n").
The full solution:
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");
}
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
