# Scala algorithm: Check if a String is a palindrome

## Algorithm goal

A String is a palindrome when reversing it yields it itself. For example, 'ab' is not a palindrome, but 'aba', and 'abba' are.

This problem is similar to CheckArrayIsAPalindrome and CheckNumberIsAPalindrome.

## Test cases in Scala

``````assert(!isPalindrome("abcd"))
assert(isPalindrome("abcdcba"))
assert(isPalindrome("abcddcba"))
assert(isPalindrome(Array(1).toIndexedSeq))
assert(!isPalindrome(Array(1, 2).toIndexedSeq))
``````

## Algorithm in Scala

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

## Explanation

Like in CheckArrayIsAPalindrome, note that we can remap an Array to its reverse without allocating a new array (efficient!).

The same can, in fact, be done for a String, as String meets a specification of 'IndexedSeq' (which is also met by an Array). (this is Â© from www.scala-algorithms.com)

To our advantage, we utilise the fact that a palindrome is its reverse, and that 'IndexedSeq' can be converted into a view, and then re-mapped into that reverse.

## Scala concepts & Hints

1. ### 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")
``````
2. ### View

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

3. ### Zip

'zip' allows you to combine two lists pair-wise (meaning turn a pair of lists, into a list of pairs)

It can be used over Arrays, Lists, Views, Iterators and other collections.

``````assert(List(1, 2, 3).zip(List(5, 6, 7)) == List(1 -> 5, 2 -> 6, 3 -> 7))

assert(List(1, 2).zip(List(5, 6, 7)) == List(1 -> 5, 2 -> 6))

assert(List(5, 6).zipWithIndex == List(5 -> 0, 6 -> 1))

assert(List(5, 6).zipAll(List('A'), 9, 'Z') == List(5 -> 'A', 6 -> 'Z'))

assert(List(5).zipAll(List('A', 'B'), 1, 'Z') == List(5 -> 'A', 1 -> 'B'))
``````

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

## How our 100 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.

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

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

### Explore the 22 most useful Scala concepts

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

## Subscribe to Scala Algorithms

Maximize your Scala with disciplined and consistently unit-tested solutions to 100+ algorithms.

Use it from improving your day-to-day data structures and Scala; all the way to interviewing.