Problem overview
The method repeatLastChar receives a string s and a whole number n from 1 to 10. It returns the last character of s written n times in a row.
For s = "banana" and n = 3, the last character is a, so the result is "aaa". For "Wow!" and n = 2, the last character is the exclamation mark, so the result is "!!". For "z" and n = 1, the result is "z".
Approach
Two small tools solve this. A negative index counts from the end of a string, so s[-1] is the last character whatever the length. And multiplying a string by an integer repeats it: "ab" * 3 is "ababab". Combined, s[-1] * n is the whole answer.
The two tools are independent. The index decides which character you repeat, and the number decides how many times. Because n is only a count, the same code works for n = 1, where the result is the character itself, and for n = 10, where it is ten copies in a row.
Step by step
- Get the last character with
s[-1]. It is a string of length 1. - Repeat it by multiplying with
n. - Return the result.
The solution in Python
class Solution:
def repeatLastChar(self, s: str, n: int) -> str:
return s[-1] * n
Some details worth knowing:
- The
*operator works between a string and an integer, in either order:3 * "a"and"a" * 3are the same. The repeated copies are joined into one new string. - Multiplying by
0gives an empty string. The constraints keepnat 1 or more, so the lesson never asks for that. - The character can be anything: a letter, a digit, a space inside the string, or punctuation.
Repeating strings
String repetition is new here, so a few results are worth seeing. "ab" * 3 is "ababab", and 3 * "ab" gives the same text. The whole string is repeated, which is why the order of the two operations matters: "banana"[-1] * 3 is "aaa", but "banana" * 3 is "bananabananabanana". Pick the character first, then repeat it. Multiplying by a string instead of an integer is an error: "r" * "4" raises TypeError: can't multiply sequence by non-int of type 'str'.
The same idea with named variables
Splitting the work into two lines shows both tools separately. For s = "Wow!" the code below prints ! and then !!:
s = "Wow!"
ch = s[-1]
print(ch)
print(ch * 2)
The first line picks the character, and the second repeats it. When a result looks wrong, printing the intermediate value tells you which of the two steps went astray: the character you picked, or the number of repeats.
The last character can be any kind of character, such as the ! in "Wow!". The code never looks at what kind of character it is. It only picks the one at index -1 and repeats it, so letters, digits and punctuation are all treated the same way.
Edge cases and common mistakes
- A one-character string.
s[-1]ands[0]are the same character, and the code still works. - Using
s[len(s)]. That index is one past the end and raisesIndexError. Use-1. - Using the first character,
s[0], instead of the last. For"banana"that would give"bbb". - Multiplying the whole string.
s * nrepeats all ofs, so"banana" * 3is"bananabananabanana". Index first, then multiply.
Testing
"banana",3gives"aaa"."Wow!",2gives"!!"."z",1gives"z".
