Problem overview
GrepInvert is the -v flag of grep: it returns the lines that do not contain the pattern. It takes string[] lines and a pattern, keeps the lines without the pattern (case-sensitive) in their original order, and joins them with \n. If every line contains the pattern, the result is the empty string.
For pattern "err" and lines ["error", "ok", "another error", "fine"], the lines "error" and "another error" contain err and are dropped. What is left is "ok" and "fine", so the result is "ok\nfine".
Approach
It is the plain search with the test flipped. A search keeps a line when line.Contains(pattern) is true. This one keeps it when that is false. Everything else, the list of results, the loop and the joined return value, stays the same, so the only new idea is the ! operator, which turns true into false and back.
One difference in the signature: the lines come as an array, string[], not a List<string>. A foreach loop reads an array in the same way it reads a list. The result is still a List<string>, because the number of kept lines is unknown in advance and an array cannot grow.
Step by step
- Create
result, an emptyList<string>, for the lines that stay. - Loop over
lineswithforeach. - Test with
!line.Contains(pattern). The!in front makes the condition true for lines without the pattern. - Add the line to
resultinside theif. - Return
string.Join("\n", result)after the loop.
The solution in C#
class Solution {
public string GrepInvert(string[] lines, string pattern) {
List<string> result = new List<string>();
foreach (string line in lines) {
if (!line.Contains(pattern)) {
result.Add(line);
}
}
return string.Join("\n", result);
}
}
The details worth knowing:
- Array and list differ in size and growth. An array, like
string[], has a fixedLength. AListhasCountandAdd. Here the input is an array and the output collection is a list, andstring.Joinaccepts both. - The
!binds to the call.!line.Contains(pattern)negates the result ofContains. The condition reads "the line does not contain the pattern". - Plain text, case-sensitive.
"Error".Contains("err")isfalse, so with pattern"err"the line"Error"is kept by this method. - The order is preserved because the loop reads the array from the first line to the last and adds in that order.
- Empty result. When nothing is added,
string.Joinof an empty list is"", which is the required answer for the case where every line matches.
Complexity
Let n be the number of lines and m the length of the longest. Time: one search per line, so it grows with n times m (times the pattern length in the worst case). Space: the result list holds up to n references to lines that already exist, and the joined string is a new string of about the combined length of the kept lines.
Edge cases and common mistakes
- Forgetting the
!. Without it you get the plain search, and the example would return"error\nanother error". - Every line matches. The result is
"", not a null or a message. - No line matches. Then every line is kept.
- Using
lines.Count. An array hasLength;Countis for lists, so it does not compile. The solution needs neither, becauseforeachhandles it. - Case.
"ERR"does not match"error", so with pattern"ERR"every line of the example stays.
Testing
- Pattern
"err"with["error", "ok", "another error", "fine"]gives"ok\nfine". - Pattern
"a"with["a", "ba"]gives"". - Pattern
"x"with["a", "b"]gives"a\nb".
