# Scala algorithm: Count number of changes (manipulations) needed to make an anagram with an efficient foldLeft

## Algorithm goal

Count number of changes needed to make. This problem is similar to:

• On HackerRank: You must split it [a string] into two contiguous substrings, then determine the minimum number of characters to change to make the two substrings into anagrams of one another.
• Find if two Strings are k-anagrams of each other (in this case, the two sides are the split of the string).

One string is an anagram of another if they have the same number of each character, not necessarily in the same order - for example `abcc` is an anagram of `accb` and vice-versa.

`ab` is composed of `a` and `b`, and exchanging `a` to `b` is enough to create an anagram - ie 1 change of letter is enough.

`abc` is not possible to create anagrams of because it cannot be split.

`poeq` requires 2 changes, to create an anagram, for example to `pqpq`, `popo`, `eoeo` and so forth.

## Test cases in Scala

``````assert(anagramChanges("aaabbb") == Some(3))
assert(anagramChanges("ab") == Some(1))
assert(anagramChanges("mnop") == Some(2))
assert(anagramChanges("xyyx") == Some(0))
assert(anagramChanges("xaxbbbxx") == Some(1))
assert(anagramChanges("abc") == None)
``````

## Algorithm in Scala

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

## Explanation

Once we split a string into 2 parts, we are left with 2 strings. Number of changes required is effectively the number of characters that are not in common.

Originally, this solution was written with a MultiSet, but there is a cleaner, more efficient approach: (this is Â© from www.scala-algorithms.com)

Because the set of Char is finite, we can use a static array-like structure, such as a Vector, to do the counting, rather than a HashMap.

If the letter 'a' is in one half of the string, and is also in the other half, that means the total number of changes needed would be '+ 1 - 1 = 0'. So, we can say that any letter on the left part of the string has a score of +1, and any side on the right has -1.

## Scala concepts & Hints

1. ### foldLeft and foldRight

A 'fold' allows you to perform the equivalent of a for-loop, but with a lot less code.

``````def foldMutable[I, O](initialState: O)(items: List[I])(f: (O, I) => O): O =
items.foldLeft(initialState)(f)
``````
2. ### Option Type

The 'Option' type is used to describe a computation that either has a result or does not. In Scala, you can 'chain' Option processing, combine with lists and other data structures. For example, you can also turn a pattern-match into a function that return an Option, and vice-versa!

``````assert(Option(1).flatMap(x => Option(x + 2)) == Option(3))

assert(Option(1).flatMap(x => None) == None)
``````
3. ### 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")
``````
4. ### Stack Safety

Stack safety is present where a function cannot crash due to overflowing the limit of number of recursive calls.

This function will work for n = 5, but will not work for n = 2000 (crash with java.lang.StackOverflowError) - however there is a way to fix it :-)

In Scala Algorithms, we try to write the algorithms in a stack-safe way, where possible, so that when you use the algorithms, they will not crash on large inputs. However, stack-safe implementations are often more complex, and in some cases, overly complex, for the task at hand.

``````def sum(from: Int, until: Int): Int =
if (from == until) until else from + sum(from + 1, until)

def thisWillSucceed: Int = sum(1, 5)

def thisWillFail: Int = sum(1, 300)
``````
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

## 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.