# Scala algorithm: Reverse a String's words efficiently

Published

## Algorithm goal

Efficiently, reverse a String's words. For example, 'Hello new world' becomes 'world new Hello'.

## Test cases in Scala

``````assert(reverseWords("").toList == List(""))
assert(reverseWords("A").toList == List("A"))
assert(reverseWords("AA").toList == List("AA"))
assert(reverseWords("AA BB").toList == List("BB", "AA"))
assert(reverseWords("AA  BB").toList == List("BB", "", "AA"))
assert(reverseString("cannot") == "cannot")
assert(reverseString("") == "")
assert(reverseString(" a b ") == " b a ")
assert(reverseString("AA BB") == "BB AA")
assert(reverseString("cannot do") == "do cannot")
assert(
reverseString("cannot do without Scala") == "Scala without do cannot"
)
``````

## Algorithm in Scala

34 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!

## Explanation

While this may look a long-winded solution, it is very explicit and also streaming-based.

An efficiency portion of this algorithm comes from the fact that rather than appending every character that we come across to an existing string, and thus ending up with allocations of a new String per character, we can extract out the positions of the spaces, and come up with the shape of our target String by describing it in terms of the edges and where the spaces are. (this is Â© from www.scala-algorithms.com)

## Scala concepts & Hints

1. ### Collect

'collect' allows you to use Pattern Matching, to filter and map items.

``````assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")
``````
2. ### Pattern Matching

Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.

``````assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")
``````
3. ### Sliding / Sliding Window

Get fixed-length sliding sub-sequences (sliding windows) from another sequence

4. ### State machine

A state machine is the use of `sealed trait` to represent all the possible states (and transitions) of a 'machine' in a hierarchical form.

5. ### View

The `.view` syntax creates a structure that mirrors another structure, until "forced" by an eager operation like .toList, .foreach, .forall, .count.

# Scala Algorithms: The most comprehensive library of algorithms in standard pure-functional Scala

### Study our 89 Scala Algorithms: 6 fully free, 89 published & 0 upcoming

Fully unit-tested, with explanations and relevant concepts; new algorithms published about once a week.

### Explore the 21 most useful Scala concepts

To save you going through various tutorials, we cherry-picked the most useful Scala concepts in a consistent form.

## Register now (free)

Register with GitHub

## How the algorithms look

1. A description/goal of the algorithm.
2. An explanation with both Scala and logical parts.
3. A proof or a derivation, where appropriate.
4. Links to Scala concepts used in this specific algorithm, also unit-tested.
5. An implementation in pure-functional immutable Scala, with efficiency in mind (for most algorithms, this is for paid subscribers only).
6. Unit tests, with a button to run them immediately in our in-browser IDE.