Start learning free

LESSON 71 OF 285 · STRINGS - CHALLENGES

Repeat Last Char in Java: substring and 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

    JAVA · STRINGS - CHALLENGES

    Repeat Last Char in Java, explained

    Problem overview

    The method repeatLastChar receives a string s and a number n. It returns a new string made of the last character of s, written n times in a row. The string has at least one character, it can contain letters, digits, spaces and punctuation, and n is between 1 and 10.

    For s = "banana" and n = 3 the last character is a, so the result is "aaa". For "Wow!" and 2 it is "!!", because any character can be the last one. For "z" and 1 the only character is the last, so the result is "z".

    Approach: find the last character, then repeat it

    There are two jobs: pick out the last character, and write it several times. Java has a ready-made tool for the second one, the String method repeat(int count). It returns a string made of the text written count times in a row, so "ab".repeat(3) is "ababab".

    The detail to notice is that repeat exists on String but not on char. The method s.charAt(s.length() - 1) returns the last character as a char, a primitive value, and a primitive has no methods, so s.charAt(...).repeat(n) doesn't compile. The way out is to take the last character as a one-character String with substring.

    Indexes in a string run from 0 to length() - 1, so the last character sits at s.length() - 1. With a single argument, substring runs from that index to the end of the string, which is exactly one character.

    Step by step

    1. Take the last character as a string. s.substring(s.length() - 1) starts at the last index and runs to the end. For "banana" it is "a".
    2. Repeat it n times. Call .repeat(n) on that string. Because substring returns a String, the call can be chained straight onto it.
    3. Return the result. The chained expression is the answer, so the method returns it directly.

    The solution in Java

    class Solution {
        public String repeatLastChar(String s, int n) {
            return s.substring(s.length() - 1).repeat(n);
        }
    }
    

    Running the three examples prints:

    banana, 3 -> aaa
    Wow!, 2 -> !!
    z, 1 -> z
    

    Some details about the pieces:

    • repeat is available since Java 11, so it works on Java 21. It doesn't change the original string: strings are immutable and repeat returns a new one.
    • Method chaining. a.b().c() calls c on the value that b returned. The order here is slice first, then repeat.
    • The result is never empty for a valid input. With n from 1 to 10, the output has between 1 and 10 characters.

    Edge cases and common mistakes

    • Using charAt with repeat. This is the compile-time trap described above: a char has no repeat method. Wrapping it, as in String.valueOf(s.charAt(s.length() - 1)).repeat(n), works but is longer than a slice.
    • Off-by-one on the index. s.substring(s.length()) starts after the last character and returns the empty string, so every call would produce "". Subtract 1 to land on the last character.
    • Repeating the wrong thing. s.repeat(n) repeats the whole string, giving "bananabananabanana" for the first example.
    • A single-character string. "z" has length 1, so the start index is 0 and substring(0) is the whole string. That is the third example.
    • Zero and negative counts. "r".repeat(0) returns the empty string, but a negative count such as "x".repeat(-1) throws an IllegalArgumentException with the message count is negative: -1. The constraints keep n between 1 and 10, so neither case arises here.
    • Trailing spaces. The lesson's strings never start or end with a space, so the last character is always visible. In general a trailing space would simply be repeated.

    Testing

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