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
- Take the last character as a string with
s.substring(s.length - 1). With a single argument,substringstarts at that index and runs to the end, which is one character here. - Multiply it by
nto repeat it. - 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 * Intcomes 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
nbetween 1 and 10, so the result is never empty here. - Calling
.toStringons.lastwould also give a string to multiply, ands.last.toString * ngives 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. AChartimes anIntis a number, as shown above, and the method's return type isString, 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 iss.length - 1. - Swapping the operands.
n * s.substring(...)doesn't compile:Inthas no*that takes aString. - 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"with3gives"aaa"."Wow!"with2gives"!!"."z"with1gives"z".
