Problem overview
The function burrrrp receives a number n and returns the sound of a burp: "bu", then n letters r, then "p".
For n = 4 the answer is "burrrrp". For n = 1 it is "burp", and for n = 7 it is "burrrrrrrp".
Approach
Only the number of r letters changes. "bu" and "p" are always the same, so the result is a fixed start, a repeated middle and a fixed end. In Kotlin, "r".repeat(n) returns "r" written n times, and + joins the three parts.
Step by step
- Start with the fixed text
"bu". - Add
"r".repeat(n), the middle part. - Add the fixed ending
"p"and return the result.
The solution in Kotlin
class Solution {
fun burrrrp(n: Int): String {
return "bu" + "r".repeat(n) + "p"
}
}
Details worth knowing:
repeatis a function onString."ab".repeat(3)is"ababab". It builds a new string and doesn't change the original.- Zero and negative counts.
"r".repeat(0)is the empty string, soburrrrp(0)would give"bup". A negative count throws anIllegalArgumentException. The lesson keepsnbetween 1 and 20. - Use a string, not a character.
"r"has double quotes, so it is aString. A single-quoted'r'is aChar, which has norepeat. - A template works too.
"bu${"r".repeat(n)}p"builds the same text.
Tracing the examples
For n = 4: "r".repeat(4) is "rrrr". The three parts "bu", "rrrr" and "p" join into "burrrrp".
For n = 1: "r".repeat(1) is "r", and the result is "burp", the word with a single r.
For n = 7: the middle is seven r letters, and the result is "burrrrrrrp".
The pattern is worth remembering: when part of the output is fixed and part depends on a number, write the fixed parts as literals and compute only the changing part. The same shape, literal plus repeated piece plus literal, fits many string-building exercises.
The largest input, n = 20, produces a middle of 20 letters. repeat handles that in one call, with no loop to write and no counter to get wrong.
Where the pieces come from
Each part of the result has its own source. The two literals are text you type. The middle is computed from the input, so it is the only place where n appears in the code. Keeping the number in one place makes the function easy to check: if the output has the wrong number of r letters, the mistake can only be in the argument to repeat.
Why not a loop
A beginner's first idea is often a loop that appends one r at a time to a variable. That works, but it needs a mutable variable, a counter and a condition, and each of them is a place for an off-by-one mistake. repeat does the same work in one expression. The result is a new String built in a single call, which also keeps the function body to one line.
Loops are still the right tool when each step differs from the last. Here every step adds the same letter, so a library function that says "this text, n times" matches the task exactly.
Edge cases and common mistakes
- Repeating the wrong piece.
"bur".repeat(n)repeats thebanduas well. Only therbelongs in the repeat. - Off by one.
repeat(n - 1)gives onertoo few: forn = 4the result would be"burrrp". - Smallest input. With
n = 1the result is"burp", which already has oner.
Testing
n = 4gives"burrrrp".n = 1gives"burp".n = 7gives"burrrrrrrp".
