Start learning free

LESSON 174 OF 348 · STRINGS

Reverse Words in a String in TypeScript: split, reverse, join

Practice O(n) Read the full explanation

Given an input string s, reverse the order of the words.

A word is defined as a sequence of non-space characters. The words in s will be separated by one space for simplicity.

Return a string of the words in reverse order concatenated by a single space.

Example:

Input:  input = "sky is blue"

Output:  "blue is sky"
STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS

    Reverse Words in TypeScript, explained

    Problem overview

    The method reverseWords receives a string, input, made of words separated by exactly one space. A word is a run of characters that are not spaces. The method returns a new string with the same words in the opposite order, again separated by single spaces.

    For "sky is blue" the words are sky, is and blue. In reverse order they are blue, is, sky, so the answer is "blue is sky". The letters inside each word keep their order. Only the order of the words changes.

    Approach: turn the words into an array

    Strings can't be edited, but arrays can, and the standard library already has methods to move between the two. The plan has three moves:

    1. Cut the string into words, so you have an array of strings.
    2. Reverse the array.
    3. Glue the words back together with a space between them.

    The statement promises exactly one space between words. That promise is what makes the plan so short. Splitting on a single space gives exactly the words, with no empty strings in between, and joining with a single space restores the original spacing.

    Step by step

    1. Split. Call input.split(" ") and store the result in words, annotated as string[]. It is declared with const. That only stops the variable from being pointed at a different array, and the array's contents can still be rearranged.
    2. Reverse in place. words.reverse() rearranges the elements of the array itself. It also returns the array, but since words already refers to the same array, the return value isn't needed.
    3. Join and return. words.join(" ") builds one string from the elements with a single space between each pair. Return it directly.

    The solution in TypeScript

    class Solution {
        reverseWords(input: string): string {
            const words: string[] = input.split(" ");
            words.reverse();
            return words.join(" ");
        }
    }
    

    The TypeScript details worth a closer look:

    • const does not freeze the array. const fixes which array words refers to. Calling words.reverse() changes what is inside that array, and that is allowed. Writing words = [] afterwards would be an error.
    • The annotation is erased. string[] tells the compiler what split returns, and it would infer that type anyway. In the compiled JavaScript there is no trace of it.
    • reverse mutates. Unlike slice or join, reverse changes the array it is called on. That is why the second line has no assignment. Writing words = words.reverse() would run, but it adds nothing.
    • The separator is a string. split(" ") cuts at each single space. split("") with an empty string would instead cut between every character, which is a different job.
    • join takes the separator. Leaving it out, words.join(), would use commas instead of spaces.

    Complexity

    Let n be the length of input.

    Time: O(n). Splitting reads every character once. Reversing the array swaps about half as many elements as there are words, which is at most n. Joining copies every character once. The three steps run one after another.

    Space: O(n). The array of words holds all the characters of the input except the spaces, and the joined result holds them all again.

    Edge cases and common mistakes

    • A single word. "hello" splits into an array with one element. Reversing it changes nothing, and the result is "hello".
    • The empty string. "".split(" ") gives [""], an array holding one empty string, and joining it gives "".
    • Extra spaces. The statement rules them out, and this solution relies on that. If the input had leading, trailing or repeated spaces, splitting would produce empty strings and they would be reversed too: " a b " would come back as " b a ". Handling such input needs extra work, such as splitting on a regular expression and filtering out the empty pieces.
    • Reversing the characters instead. Reversing the whole string flips the letters of every word as well: "sky is blue" would become "eulb si yks".
    • Forgetting to return the joined string. The array is not a string. Returning words instead of words.join(" ") is a compile-time error here because the method must return a string.
    • Joining with the wrong separator. join("") glues the words with nothing between them: "blueissky".

    Testing

    • "sky is blue" gives "blue is sky".
    • "hello" gives "hello".
    • "a b" gives "b a".
    • "one two three four" gives "four three two one".
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free