Problem overview
This version adds the -v flag of grep. grepInvert(lines: Array[String], pattern: String): String returns the lines that do not contain pattern as a substring (case-sensitive), in their original order, joined by \n. If every line contains the pattern, the result is the empty string.
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
The first grep version kept the lines for which contains was true. This one keeps the lines for which it is false. The whole change is one character: a ! in front of the test.
Everything else carries over. Loop over the lines, append the lines that pass the test to a growable ArrayBuffer, and join the buffer with mkString("\n") at the end. The order of the lines is kept because lines are appended as the loop reaches them. The result is empty when every line contains the pattern, because then nothing is ever appended, and joining an empty buffer gives "".
Notice that "doesn't contain" is the exact complement of "contains": each line goes to exactly one of the two outputs. Running the previous function and this one on the same input would split the lines into two groups that together make up the whole input.
Step by step
- Create the buffer. A
valnamedresultholds an emptyscala.collection.mutable.ArrayBuffer[String]. - Loop over the lines with
for (line <- lines). - Negate the test. The
ifuses!line.contains(pattern), which is true when the pattern does not appear anywhere in the line. - Keep the line. Append
linetoresultwith+=. Only lines without the pattern get here. - Join and return with
result.mkString("\n").
The solution in Scala
class Solution {
def grepInvert(lines: Array[String], pattern: String): String = {
val result = scala.collection.mutable.ArrayBuffer[String]()
for (line <- lines) {
if (!line.contains(pattern)) {
result += line
}
}
return result.mkString("\n")
}
}
Details to notice:
!is the logical NOT operator. It applies to the whole call that follows it,line.contains(pattern), and gives aBoolean.- The comparison is case-sensitive, so a line with
"ERR"is not removed by the pattern"err". - An empty pattern is contained in every line, so with
pattern = ""nothing survives and the result is"". result += lineworks on avalbecause the buffer itself is modified, not reassigned.
Complexity
Each line is searched once, so the time is proportional to the total size of the input (and the pattern length in the worst case of each search). The buffer holds references to at most all n lines, so the extra space is O(n) plus the joined result.
Edge cases and common mistakes
- Forgetting the
!. The code would then be the first version and return the matching lines. - All lines match. The result is
"", not a blank line; the buffer is empty somkStringhas nothing to join. - No line matches. Every line is kept and joined by newlines.
- Case mix-ups. This version doesn't ignore case, so
"Error"survives the pattern"err". - Changing the order. Keep lines in the order they appear; the loop does this naturally.
Testing
| lines | pattern | Result |
|---|---|---|
Array("error", "ok", "another error", "fine") | "err" | "ok\nfine" |
Array("error", "another error") | "err" | "" |
Array("a", "b") | "" | "" |
