Problem overview
This version adds the -i flag to the small grep. The function grepCaseInsensitive(lines: List<String>, pattern: String): String returns every line that contains pattern as a substring while ignoring letter case, joined by newline characters. The lines that come back keep their original capitalization, and when nothing matches the result is the empty string. The lines contain only ASCII characters.
With pattern = "ERR" and lines = ["Error: 1", "ok"], the result is "Error: 1". The pattern is in capitals and the line isn't, yet it matches, and the line is returned exactly as it was written.
Approach: compare in one case, return the original
A plain contains is case-sensitive, so "Error: 1".contains("ERR") is false. The fix is to bring both sides to the same case before comparing. If the pattern and the line are both lowercase, then ERR becomes err and finds the start of error: 1.
The important detail is what you store. The lowercase copy of the line exists only for the test. The list of results receives the original line, so capitals survive. If you added the lowercase copy instead, "Error: 1" would come back as "error: 1" and the lesson's tests would fail.
The pattern is the same for every line, so it is lowercased once before the loop. Each line has to be lowercased inside the loop, because each one is different.
Step by step
- Create the result list.
mutableListOf<String>()goes in avalnamedresult. The variable never changes, only the list's contents do. - Lowercase the pattern once.
pattern.lowercase()goes in avalnamedlowerPattern, placed outside the loop so it isn't recomputed for every line. - Loop over the lines with
for (line in lines), closing the brace immediately. - Compare in lowercase. The
ifchecks whetherline.lowercase()containslowerPattern. Both sides are lowercase now, soERRfindsError, whilelineitself is untouched. - Keep the original line. Inside the
if,result.add(line)stores the line with its capitals. - Join and return.
result.joinToString("\n")puts a newline between the kept lines, and gives the empty string when the list is empty.
The solution in Kotlin
class Solution {
fun grepCaseInsensitive(lines: List<String>, pattern: String): String {
val result = mutableListOf<String>()
val lowerPattern = pattern.lowercase()
for (line in lines) {
if (line.lowercase().contains(lowerPattern)) {
result.add(line)
}
}
return result.joinToString("\n")
}
}
Kotlin details to know:
lowercase()returns a new string. Strings are immutable in Kotlin, so callingline.lowercase()never changesline. That is why the original is still available to add toresult.- Older Kotlin code uses
toLowerCase().lowercase()replaced it as the current name. - The standard library has a built-in shortcut:
line.contains(pattern, ignoreCase = true)istruefor"Error: 1"and"ERR". The lesson spells the idea out withlowercase()so that you see what ignoring case means. - Writing
valforlowerPatterndocuments that it is computed once and never reassigned.
Edge cases and common mistakes
- Returning the lowercase line. The output must keep the original casing:
["Error: 1", "ok"]with"ERR"givesError: 1, noterror: 1. - Lowercasing only one side. Writing
line.contains(lowerPattern)would miss"Error"if only the pattern were lowercased, because the line's capitalEwouldn't matche. Both sides must be in the same case. - Letters in the middle of a word. The match is a substring match, so
"err"also finds"tERRor". - No matches. An empty result list joins to
"". - Non-ASCII text. Case rules for other alphabets are more complicated, and this lesson's input avoids them.
Testing
These results come from running the reference solution:
| lines | pattern | result |
|---|---|---|
["Error: 1", "ok"] | "ERR" | Error: 1 |
["Error: 1", "ok", "tERRor"] | "err" | Error: 1 and tERRor, on two lines |
["a"] | "b" | the empty string |
