Start learning free

LESSON 72 OF 285 · STRINGS - CHALLENGES

Burrrrp in Java: Building a String with repeat

Practice Read the full explanation

Given a number n, return the sound of a burp with n letters r: "bu", then n letters r, then "p".

 

Example 1:

Input: n = 4
Output: "burrrrp"
Explanation: "bu", then 4 letters r, then "p". That's the burp in this lesson's title.

Example 2:

Input: n = 1
Output: "burp"
Explanation: The shortest burp has a single r.

Example 3:

Input: n = 7
Output: "burrrrrrrp"
Explanation: "bu", then 7 letters r, then "p".

 

Constraints:

  • n is a whole number from 1 to 20.
  • The result is all lowercase.
STEP 1 OF 1
    Ctrl + Enter to check

    JAVA · STRINGS - CHALLENGES

    Burrrrp in Java, explained

    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

    1. Start with the fixed beginning. The literal "bu" opens the expression.
    2. Add the repeated letters. "r".repeat(n) produces exactly n letters. Use a string literal, in double quotes, because repeat is a method of String.
    3. Add the fixed ending. The literal "p" closes the burp.
    4. Return the whole expression. The method needs no variables, so a single return is 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 + 3 characters: two for "bu", n for the middle and one for "p". For n = 7 that 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 for n = 1, "burp", to check the count.
    • Repeating the wrong string. Repeating "bur" repeats the b and u as well, so the text no longer has the shape bu, r's, p. Only the single letter belongs in repeat.
    • Mixing up char and String. 'r'.repeat(n) doesn't compile, since single quotes make a char, which has no methods. Double quotes make the string that repeat needs.
    • 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 an IllegalArgumentException.
    • Case. Everything is lower case. Writing "Bu" or "P" fails the lesson's tests.

    Testing

    • n = 4 gives "burrrrp".
    • n = 1 gives "burp".
    • n = 7 gives "burrrrrrrp".
    Keep going with Java1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free