Start learning free

LESSON 200 OF 415 · STRINGS

Reverse String in Swift: O(n) Time with a Character Array

Practice O(n) Read the full explanation

Given a string, write a function that reverses that string without using built-in functions or libraries.

Example:

Input:  "hello"

Output: "olleh"

Note:

Your algorithm should run in O(n) time and use O(n) extra space.


STEP 1 OF 4
    Ctrl + Enter to check

    SWIFT · STRINGS

    Reverse String in Swift, explained

    Problem overview

    The method reverse(_ input: String) -> String returns the characters of input in the opposite order: "hello" becomes "olleh". This lesson asks for O(n) time and O(n) extra space.

    Approach: read backwards, append forwards

    The simple way to reverse is to prepend each character to the result, but that copies the growing result every time and takes O(n²). The faster way flips the roles: visit the input from the last character to the first, and append each one to the end of the result. Appending to the end of a Swift string takes amortized constant time, so the whole loop is linear.

    There is one obstacle. A Swift string cannot be indexed by integer, so you cannot ask for "the character at position i". The fix is to copy the characters into an array once, in O(n), and index the array.

    Step by step

    1. Prepare the result and the array. reversedString is a var holding "". characters is a let set to Array(input), a [Character] that can be indexed.
    2. Loop backwards over the indexes. The indexes run from characters.count - 1 down to 0, so use stride(from: characters.count - 1, through: 0, by: -1). through: includes 0, while to: would stop before it and lose the first character.
    3. Append. Add String(characters[i]) to the end of reversedString with +=. The element is a Character, so it is converted to a String first.
    4. Return the result.

    The solution in Swift

    class Solution {
        func reverse(_ input: String) -> String {
            var reversedString = ""
            let characters = Array(input)
            for i in stride(from: characters.count - 1, through: 0, by: -1) {
                reversedString += String(characters[i])
            }
            return reversedString
        }
    }
    

    The Swift details worth a closer look:

    • Counting down. Swift's range syntax only goes upward: 5...0 stops the program, because a range cannot end below its start. stride accepts a negative step, so it is the tool for counting down.
    • The empty string. Then characters.count - 1 is -1, and a stride from -1 through 0 with step -1 is empty. The loop does not run and the method returns "".
    • No overflow worry. The index i is a plain Int, which can hold -1, unlike an unsigned type. Counting down never goes wrong at 0.
    • Characters stay whole. Each array element is one Character, so a letter with a combining accent keeps its accent when the order is reversed.
    • The array is a copy. Changing the array would not change input, and this method only reads it.

    Complexity

    Time: O(n). Building the array visits each character once. The loop runs n times, and each append takes amortized constant time. The total is linear.

    Space: O(n). The array holds n characters and the result holds another n. Constants aside, the extra memory grows with the input.

    Edge cases and common mistakes

    • Starting at characters.count. The last valid index is count - 1. Starting one higher reads past the end of the array and stops the program.
    • Using to: instead of through:. The stride then stops before 0, and the first character of the input is missing from the result.
    • Appending characters[i] without String(...). A Character cannot be added to a String with +=; the compiler reports an error.
    • Trying input[i]. Strings have no integer subscript, which is why the array exists.

    Testing

    • "hello" gives "olleh".
    • "" gives "".
    • "a" gives "a".
    • "ab" gives "ba".
    • "ab cd" gives "dc ba".
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free