Start learning free

LESSON 72 OF 291 · STRINGS - CHALLENGES

Repeat Last Char in Scala: Repeating a String with *

Practice Read the full explanation

Given a string s and a number n, return a new string made of the last character of s, written n times in a row.

 

Example 1:

Input: s = "banana", n = 3
Output: "aaa"
Explanation: The last character of "banana" is "a". Written 3 times, it gives "aaa".

Example 2:

Input: s = "Wow!", n = 2
Output: "!!"
Explanation: The last character can be any character: here it is the exclamation mark.

Example 3:

Input: s = "z", n = 1
Output: "z"
Explanation: A string of one character: its last character is "z", written once.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
  • n is a whole number from 1 to 10.
STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · STRINGS - CHALLENGES

    Repeat Last Char in Scala, explained

    Problem overview

    The method repeatLastChar receives a string s and a number n, and returns a string made of the last character of s written n times in a row. For s = "banana" and n = 3 the answer is "aaa". For "Wow!" and 2 it is "!!", because the last character can be any character, punctuation included. For "z" and 1 it is "z": a one-character string has its last character at index 0.

    Approach

    There are two jobs: get the last character, then repeat it. The last character is at index s.length - 1. Repeating is done by a Scala feature that is new at this point: a String can be multiplied by an Int. The result is that string written that many times, so "ab" * 3 is "ababab".

    The multiplication only repeats a String. If the last character is read with charAt, the result is a Char, and * on a Char is plain arithmetic on its character code. With s = "hello", s.charAt(s.length - 1) * 3 gives the number 333 (the code of o is 111), not "ooo". So the last character has to be kept as a one-character string, and a slice does that.

    Step by step

    1. Take the last character as a string with s.substring(s.length - 1). With a single argument, substring starts at that index and runs to the end, which is one character here.
    2. Multiply it by n to repeat it.
    3. Return the result. The expression is the whole method body.

    The solution in Scala

    class Solution {
        def repeatLastChar(s: String, n: Int): String = {
            return s.substring(s.length - 1) * n
        }
    }
    

    A few Scala details:

    • String * Int comes from Scala's string extensions and repeats the string. The number goes on the right: the text is on the left of *.
    • The order of work is slice first, then multiply, because the method call binds tighter than the operator.
    • Repeating zero times gives the empty string. The lesson keeps n between 1 and 10, so the result is never empty here.
    • Calling .toString on s.last would also give a string to multiply, and s.last.toString * n gives the same result. The lesson uses the slice.

    Tracing an example

    For s = "banana" and n = 3, the length is 6 and the slice starts at index 5, so it is "a". Multiplied by 3, it makes "aaa". For "Wow!" the slice starts at index 3 and is "!", which repeated twice gives "!!".

    Why * and not a loop

    Repeating text is a common need, and a loop that appends one copy at a time also works. The * operator does the loop for you, so the method stays a single expression. Note the order of the operands too: the string is on the left and the count on the right. Writing the count first, as in n * "x", is a compile error, because Int has no * that accepts a string.

    Edge cases and common mistakes

    • Using charAt. A Char times an Int is a number, as shown above, and the method's return type is String, so the code would not compile.
    • Starting the slice at s.length. That gives an empty string, so the repeated result is empty too. The last valid index is s.length - 1.
    • Swapping the operands. n * s.substring(...) doesn't compile: Int has no * that takes a String.
    • One-character input. For "z" the slice starts at index 0 and is the whole string, so the method works without any special case.

    Testing

    • "banana" with 3 gives "aaa".
    • "Wow!" with 2 gives "!!".
    • "z" with 1 gives "z".
    Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free