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:
def grepInvert(self, lines: List[str], pattern: str) -> str:
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 lines where pattern not in line.
The full solution:
class Solution:
def grepInvert(self, lines, pattern):
result = []
for line in lines:
if pattern not in line:
result.append(line)
return "\n".join(result)
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
