Start learning free

LESSON 73 OF 291 · STRINGS - CHALLENGES

Burrrrp in Scala: Building a String with *

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

    SCALA · STRINGS - CHALLENGES

    Burrrrp in Scala, explained

    Problem overview

    The method burrrrp receives a number n and returns the sound of a burp with n letters r: "bu", then n times r, then "p". With n = 4 it returns "burrrrp", with n = 1 it returns "burp", and with n = 7 it returns "burrrrrrrp". The lesson keeps n between 1 and 20, and the result is all lowercase.

    Approach

    Only one part of the answer changes with the input: the run of r letters. The start, "bu", and the end, "p", are always the same. So the method is three pieces joined together, with the middle piece made by repeating the string "r" n times.

    In Scala, a String times an Int repeats the string: "r" * 4 is "rrrr". That means no loop is needed.

    This is a useful habit for any string-building task: write down the result for two or three inputs, circle what stays the same, and see which part depends on the input. Here the answers for 1 and 4 differ only in the number of r letters, so the fixed parts become literals and the changing part becomes a repetition driven by n.

    Step by step

    1. Start with the fixed text "bu".
    2. Add the repeated letters with "r" * n.
    3. Add the ending "p", and return the joined string.

    The solution in Scala

    class Solution {
        def burrrrp(n: Int): String = {
            return "bu" + "r" * n + "p"
        }
    }
    

    A few Scala details:

    • Operator order. * binds tighter than +, so "r" * n is computed first and then joined between "bu" and "p". No parentheses are needed. If you want to be explicit, you can write "bu" + ("r" * n) + "p", which means the same.
    • return. The lesson writes return explicitly. Scala also lets a method end with the expression itself, so "bu" + "r" * n + "p" alone, as the last line, returns the same string.
    • The type of n. n is an Int, which is what * on a string expects. A negative count would give an empty repetition, but the lesson never passes one.
    • Double quotes. "r" is a String. A single-quoted 'r' is a Char, and 'r' * n is a number: the code of r (114) times n.

    Tracing an example

    For n = 4, "r" * 4 is "rrrr". The pieces "bu", "rrrr" and "p" are joined into "burrrrp". For n = 1, "r" * 1 is just "r", and the result is "burp", the shortest burp. For n = 7, the result is "burrrrrrrp".

    Compared with a loop

    The same string can be built with a loop that adds one "r" at a time:

    var middle = ""
    for (i <- 1 to n) middle = middle + "r"
    "bu" + middle + "p"
    

    For n = 4 this gives "burrrrp" too, and for n = 20 the result has 23 characters. It needs a var, a loop and the range 1 to n, which includes both ends. The * version says the same thing in one expression, and there is no counter to get wrong.

    Edge cases and common mistakes

    • Counting the other letters. The number n counts only the r letters. The total length of the result is n + 3.
    • Repeating the whole word. ("bu" + "r" + "p") * n repeats the entire burp, giving "burpburpburpburp" for n = 4.
    • Using a Char. A Char repeated with * becomes a number, not text, so the method would not return a string.
    • Zero repetitions. "r" * 0 is the empty string, so n = 0 would give "bup". The lesson's constraints start at 1.

    Testing

    • 4 gives "burrrrp".
    • 1 gives "burp".
    • 7 gives "burrrrrrrp".
    • 20, the largest value, gives "burrrrrrrrrrrrrrrrrrrrp", which is 23 characters long.
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free