Problem overview
The method burrrrp receives a number n from 1 to 20 and returns the sound of a burp with n letters r: the text "bu", then n letters r, then "p". The whole result is lower case.
With n = 4 the result is "burrrrp", the burp in the lesson's title. With n = 1 it is "burp", the shortest burp. With n = 7 it is "burrrrrrrp".
Approach: fixed ends, a repeated middle
Every answer has the same three parts. The beginning "bu" and the end "p" never change. Only the middle depends on n: it is the letter r written n times. Only the middle needs any computation.
Java's String class has a method for it. "r".repeat(n) returns a new string made of "r" written n times in a row: "r".repeat(3) is "rrr". Without it you would need a loop that appends one letter per round; repeat says the same thing in one call.
The pieces are joined with the + operator. When an operand is a String, + concatenates, so "bu" + "rrrr" + "p" is "burrrrp".
Step by step
- Start with the fixed beginning. The literal
"bu"opens the expression. - Add the repeated letters.
"r".repeat(n)produces exactlynletters. Use a string literal, in double quotes, becauserepeatis a method ofString. - Add the fixed ending. The literal
"p"closes the burp. - Return the whole expression. The method needs no variables, so a single
returnis enough.
The solution in Java
class Solution {
public String burrrrp(int n) {
return "bu" + "r".repeat(n) + "p";
}
}
The three examples print:
burrrrp
burp
burrrrrrrp
Some details about this code:
- Evaluation order. Method calls bind tighter than
+, so"r".repeat(n)is computed first and the three strings are then joined left to right. - Counting the r's. The result always has
n + 3characters: two for"bu",nfor the middle and one for"p". Forn = 7that is 10 characters. - Method names can be playful. The method is called
burrrrp, with four r's, and Java doesn't mind. Use exactly that spelling, because the lesson's tests call the method by name.
The same shape appears whenever text has fixed ends and a variable middle: decide which parts never change, write them as literals, and compute only the part that depends on the input. Keeping the literals in one expression also makes it easy to compare the code with the examples, character by character.
Edge cases and common mistakes
- One r too many or too few. The first example has four r's for
n = 4. A solution that repeats"rr"or starts the count at 0 produces the wrong number of letters. Compare the expected text forn = 1,"burp", to check the count. - Repeating the wrong string. Repeating
"bur"repeats thebanduas well, so the text no longer has the shapebu, r's,p. Only the single letter belongs inrepeat. - Mixing up
charandString.'r'.repeat(n)doesn't compile, since single quotes make achar, which has no methods. Double quotes make the string thatrepeatneeds. - Zero r's.
"r".repeat(0)is the empty string, which would give"bup". The constraints start at 1, so it isn't tested, but it is a valid call. A negative count throws anIllegalArgumentException. - Case. Everything is lower case. Writing
"Bu"or"P"fails the lesson's tests.
Testing
n = 4gives"burrrrp".n = 1gives"burp".n = 7gives"burrrrrrrp".
