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
- 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". - Repeat it
ntimes. Call.repeat(n)on that string. Becausesubstringreturns aString, the call can be chained straight onto it. - 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:
repeatis available since Java 11, so it works on Java 21. It doesn't change the original string: strings are immutable andrepeatreturns a new one.- Method chaining.
a.b().c()callscon the value thatbreturned. The order here is slice first, then repeat. - The result is never empty for a valid input. With
nfrom 1 to 10, the output has between 1 and 10 characters.
Edge cases and common mistakes
- Using
charAtwithrepeat. This is the compile-time trap described above: acharhas norepeatmethod. Wrapping it, as inString.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 andsubstring(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 anIllegalArgumentExceptionwith the messagecount is negative: -1. The constraints keepnbetween 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 = 3gives"aaa".s = "Wow!",n = 2gives"!!".s = "z",n = 1gives"z".
