Multiline strings with trimIndent in Kotlin - Time & Space Complexity
We want to understand how the time to process multiline strings with trimIndent changes as the string size grows.
How does the trimming operation scale when the string has more lines?
Analyze the time complexity of the following code snippet.
val text = """
line one
line two
line three
""".trimIndent()
println(text)
This code creates a multiline string and removes common leading spaces from each line using trimIndent.
- Primary operation: The function scans each line of the string to find the smallest indent and then removes that indent from all lines.
- How many times: It processes each line twice: once to find the indent, once to trim it.
As the number of lines increases, the trimming work grows roughly in proportion to the number of lines.
| Input Size (n lines) | Approx. Operations |
|---|---|
| 10 | About 20 line checks (2 per line) |
| 100 | About 200 line checks |
| 1000 | About 2000 line checks |
Pattern observation: The work doubles if the number of lines doubles, showing a steady growth.
Time Complexity: O(n)
This means the time to trim indentation grows linearly with the number of lines in the string.
[X] Wrong: "trimIndent runs instantly no matter how big the string is."
[OK] Correct: The function must check each line to find and remove indentation, so more lines mean more work.
Understanding how string operations scale helps you reason about performance in real apps, especially when handling large text data.
"What if we used trimMargin instead of trimIndent? How would the time complexity change?"
Practice
trimIndent() function do when used with multiline strings in Kotlin?Solution
Step 1: Understand the purpose of trimIndent()
The function trimIndent() removes the common leading whitespace from every line in a multiline string.Step 2: Effect on multiline strings
This makes the string output aligned neatly without extra indentation caused by code formatting.Final Answer:
It removes the common leading whitespace from each line. -> Option DQuick Check:
trimIndent() = removes leading spaces [OK]
- Thinking trimIndent() adds spaces instead of removing
- Confusing trimIndent() with uppercase conversion
- Assuming trimIndent() splits strings into lines
Solution
Step 1: Identify multiline string syntax
Kotlin uses triple double quotes """ to create multiline strings.Step 2: Apply trimIndent() correctly
Calling trimIndent() on the multiline string removes common leading spaces.Final Answer:
val text = """Hello\nWorld""".trimIndent() -> Option AQuick Check:
Triple quotes + trimIndent() = correct syntax [OK]
- Using single quotes instead of triple quotes
- Trying to trimIndent() on a normal string
- Wrong quote count causing syntax errors
val text = """
Kotlin
is
fun
""".trimIndent()
println(text)Solution
Step 1: Analyze the multiline string with indentation
The string has 4 spaces before each line inside the triple quotes.Step 2: Effect of trimIndent()
trimIndent() removes the common 4-space indentation from all lines.Final Answer:
Kotlin is fun -> Option CQuick Check:
trimIndent() removes leading spaces = clean lines [OK]
- Expecting original indentation to remain
- Confusing trimIndent() with trim() which removes only ends
- Thinking output is a single line string
val message = """
Hello
World
"".trimIndent()
println(message)Solution
Step 1: Check triple quote syntax
Multiline strings require triple double quotes """ at start and end.Step 2: Identify the closing quotes
The code uses only two double quotes "" at the end, which is invalid syntax.Step 3: Correct the closing quotes
It should be """ to properly close the multiline string.Final Answer:
The closing triple quotes should be """ not "". -> Option BQuick Check:
Triple quotes must match at start and end [OK]
- Using two quotes instead of three to close multiline strings
- Assuming trimIndent() fixes quote errors
- Confusing trimIndent() with trimming quotes
trimIndent()?Solution
Step 1: Understand trimIndent() effect on indentation
trimIndent() removes the common leading spaces from all lines, aligning text properly.Step 2: Check each option's indentation
val fruits = """ - Apple - Banana - Cherry """.trimIndent() println(fruits) has consistent 4-space indentation on each line, so trimIndent() will remove it neatly.Step 3: Evaluate other options
val fruits = """ - Apple - Banana - Cherry """.trimIndent() println(fruits) has uneven indentation, so trimIndent() removes nothing and leaves extra spaces on some lines. val fruits = """ - Apple - Banana - Cherry """.trimIndent() println(fruits) has uneven indentation, so trimIndent() won't align all lines properly. val fruits = "- Apple\n- Banana\n- Cherry".trimIndent() println(fruits) is a normal string, trimIndent() has no effect.Final Answer:
val fruits = """ - Apple - Banana - Cherry """.trimIndent() println(fruits) -> Option AQuick Check:
Consistent indentation + trimIndent() = neat output [OK]
- Using uneven indentation causing partial trimming
- Not using triple quotes for multiline strings
- Expecting trimIndent() to fix uneven spaces
Ready to prove it?
Pass the test and this pattern moves unknown
