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
- Find the index of the last character:
s.length - 1. - Take the string that starts there with
s.substring(s.length - 1). It holds exactly one character. - 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:
repeattakes anIntand returns a new string."ab".repeat(3)is"ababab", and"ab".repeat(0)is an empty string.- Negative counts fail.
"ab".repeat(-1)throws anIllegalArgumentException. The lesson keepsnbetween 1 and 10. - Converting a
Char. If you do have aChar,s[s.length - 1].toString().repeat(n)works as well, as doess.last().toString().repeat(n). Thesubstringform avoids the extra conversion. - Methods chain left to right.
substringruns first and its result is the string thatrepeatis 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
Chardirectly.s[s.length - 1].repeat(n)doesn't compile, becauserepeatexists only on strings. - Off by one.
s.substring(s.length)is an empty string, ands[s.length]throws aStringIndexOutOfBoundsException. The last index iss.length - 1. - Repeating the whole string.
s.repeat(n)repeats all ofs."banana".repeat(3)is not"aaa". - One-character strings. For
"z",s.length - 1is 0, so the substring is the whole string, and the result is"z"repeatedntimes.
Testing
"banana", 3 gives"aaa"."Wow!", 2 gives"!!"."z", 1 gives"z".
