Extensible Grep Line Filter
Problem statement
A text file is serialized as the array lines. Return every original line selected by a simplified grep command, preserving file order and duplicate lines.
A line initially matches when it contains pattern as a contiguous substring. The unique flags in flags may appear in either order:
-imakes substring matching case-insensitive for ASCII letters.-vinverts the final match decision.
An empty pattern matches every line before optional inversion. Return original line text without normalization.
Function
grepLines(lines: String[], pattern: String, flags: String[]) → String[]Examples
Example 1
lines = ["error: disk full","INFO ready","network error"]pattern = "error"flags = []return = ["error: disk full","network error"]The first and third lines contain the case-sensitive substring error.
Example 2
lines = ["Error","all good","ERROR again"]pattern = "error"flags = ["-i"]return = ["Error","ERROR again"]The -i flag ignores ASCII letter case while returned lines keep their original text.
Example 3
lines = ["warn","ok","WARN"]pattern = "warn"flags = ["-v","-i"]return = ["ok"]Case-insensitive matching selects the two warning lines, then -v returns their complement.
Constraints
0 <= lines.length <= 10000.0 <= lines[i].length <= 500.0 <= pattern.length <= 100.0 <= flags.length <= 2.- Every line and
patterncontains printable ASCII characters. flagscontains unique values chosen from-iand-v.