Start learning free

LESSON 73 OF 285 · STRINGS - CHALLENGES

Repeat Last Char in Go: strings.Repeat and Indexing

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

    GO · STRINGS - CHALLENGES

    Repeat Last Char in Go, explained

    Problem overview

    The function repeatLastChar receives a string s and a whole number n from 1 to 10. It returns a new string made of 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". The last character can be any character: "Wow!" with n = 2 gives "!!". A one-character string such as "z" with n = 1 gives "z".

    Approach

    The task splits into two small jobs: find the last character, then repeat it. A loop that appends to a string would work, but the standard library already has the second job covered. strings.Repeat(text, n) returns text written n times in a row, so strings.Repeat("ab", 3) is "ababab".

    For the first job, the last index of a string is len(s)-1, because indexes start at 0. Reading s[len(s)-1] gives that character as a byte, a number. strings.Repeat wants a string as its first argument, so the byte must be converted with string(...) first.

    Step by step

    1. Read the last byte with s[len(s)-1].
    2. Convert it to a one-character string with string(...).
    3. Repeat it with strings.Repeat(..., n).
    4. Return the result.

    The solution in Go

    func repeatLastChar(s string, n int) string {
        return strings.Repeat(string(s[len(s)-1]), n)
    }
    

    Running it on the three examples gives these results:

    s = "banana", n = 3 -> "aaa"
    s = "Wow!",   n = 2 -> "!!"
    s = "z",      n = 1 -> "z"
    

    Details to keep in mind:

    • A byte is not a string. s[len(s)-1] has the type byte. Passing it straight to strings.Repeat is a compile error, because the function expects a string. The conversion string(...) fixes that.
    • The strings package. Repeat lives there. In a file of your own, the program needs import "strings" at the top; in the lesson's editor that import is already in place for you.
    • Counting bytes. Indexing a string reads bytes. For English letters, digits and punctuation, one character is one byte, which is what the constraints promise. A character such as é takes two bytes, and its last byte alone isn't a valid character.
    • A negative count panics. strings.Repeat("r", -1) stops the program with "strings: negative Repeat count". The constraints keep n between 1 and 10, so this can't happen here.

    The same thing with a loop

    Without strings.Repeat, a loop that adds the character n times does the job:

    func lastRepeat(s string, n int) string {
        last := string(s[len(s)-1])
        result := ""
        for i := 0; i < n; i++ {
            result += last
        }
        return result
    }
    

    Called with "banana" and 3, it also returns "aaa". Naming the last character once, in last, avoids repeating the index expression. The one-line version with strings.Repeat says the same thing with less to read.

    Edge cases and common mistakes

    • Repeating the whole string. strings.Repeat(s, n) gives "bananabananabanana" for the first example. Only the last character is repeated.
    • Using len(s) as the index. The last valid index is len(s)-1. Indexing at len(s) stops the program with an "index out of range" panic.
    • Taking the first character. s[0] would give "bbb" for "banana".
    • One-character strings. The last index is 0, the same as the first, so "z" works with no special case.
    • Punctuation. The last character isn't always a letter. For "Wow!" it is the exclamation mark, and the function repeats it just the same.

    Testing

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