Start learning free

LESSON 72 OF 291 · STRINGS - CHALLENGES

Repeat Last Char in Kotlin: String.repeat

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

    KOTLIN · STRINGS - CHALLENGES

    Repeat Last Char in Kotlin, explained

    Problem overview

    The function repeatLastChar receives a string s and a number n, and returns the last character of s written n times in a row.

    For "banana" and n = 3 the answer is "aaa". For "Wow!" and n = 2 it is "!!", since the last character can be any character. For "z" and n = 1 it is "z".

    Approach

    There are two parts: get the last character, then repeat it. Kotlin's repeat(n) is defined on strings: "ab".repeat(3) is "ababab". A Char has no repeat function, so the last character has to be a one-character String.

    s[s.length - 1] gives a Char, which is the wrong type here. s.substring(s.length - 1) gives a one-character String, because a start index alone means "to the end".

    Step by step

    1. Find the index of the last character: s.length - 1.
    2. Take the string that starts there with s.substring(s.length - 1). It holds exactly one character.
    3. Call .repeat(n) on it and return the result.

    The solution in Kotlin

    class Solution {
        fun repeatLastChar(s: String, n: Int): String {
            return s.substring(s.length - 1).repeat(n)
        }
    }
    

    Details worth knowing:

    • repeat takes an Int and returns a new string. "ab".repeat(3) is "ababab", and "ab".repeat(0) is an empty string.
    • Negative counts fail. "ab".repeat(-1) throws an IllegalArgumentException. The lesson keeps n between 1 and 10.
    • Converting a Char. If you do have a Char, s[s.length - 1].toString().repeat(n) works as well, as does s.last().toString().repeat(n). The substring form avoids the extra conversion.
    • Methods chain left to right. substring runs first and its result is the string that repeat is called on.

    Tracing the examples

    For "banana" and n = 3: s.length - 1 is 5, s.substring(5) is "a", and "a".repeat(3) is "aaa". Calling repeat on the whole word instead would give "bananabananabanana".

    For "Wow!" and n = 2: the last index is 3, the substring is "!", and the result is "!!". Digits, spaces and punctuation are characters like any other.

    For "z" and n = 1: the last index is 0, the substring is "z", and repeating it once changes nothing.

    Choosing between Char and String

    Kotlin keeps Char and String as different types, and this exercise sits right on the border. Indexing and last() give a Char, which is one character. A slice gives a String, which can be one character long. Functions such as repeat live on String, so the slice is the shortest path.

    When a compiler error mentions that a function isn't found for Char, check which type the expression really has before changing the logic. Often the fix is to convert the value with toString(), or to build the value as a string from the start.

    How repeat grows the result

    The result's length is always n, because the repeated piece has one character. That makes the output easy to predict and to test: for any valid input, repeatLastChar(s, n).length equals n, and every character equals the last character of s. The rest of s, however long, never affects the answer.

    Edge cases and common mistakes

    • Using a Char directly. s[s.length - 1].repeat(n) doesn't compile, because repeat exists only on strings.
    • Off by one. s.substring(s.length) is an empty string, and s[s.length] throws a StringIndexOutOfBoundsException. The last index is s.length - 1.
    • Repeating the whole string. s.repeat(n) repeats all of s. "banana".repeat(3) is not "aaa".
    • One-character strings. For "z", s.length - 1 is 0, so the substring is the whole string, and the result is "z" repeated n times.

    Testing

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