Scala algorithm: Count number of changes (manipulations) needed to make an anagram with foldLeft and a MultiSet
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.
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.
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.
If we're counting a "difference" between two "sets", then we need a set differentiation operation. But this set is of a particular type - we have counters, so in fact it is a Map-like type. There is a name for it and it is a MultiSet (I discovered this while working out a solution for this problem). (this is © from www.scala-algorithms.com)
The rest of the Explanation is available for subscribers.
or
'Unlimited Scala Algorithms' gives you access to all the Scala Algorithms!
Upon purchase, you will be able to Register an account to access all the algorithms on multiple devices.
Scala concepts & Hints
Collect
'collect' allows you to use Pattern Matching, to filter and map items.
foldLeft and foldRight
A 'fold' allows you to perform the equivalent of a for-loop, but with a lot less code.
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!
Pattern Matching
Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.
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.
View
The
.view
syntax creates a structure that mirrors another structure, until "forced" by an eager operation like .toList, .foreach, .forall, .count.
Algorithm in Scala
44 lines of Scala (version 2.13).
This solution is available for access!
or
'Unlimited Scala Algorithms' gives you access to all the Scala Algorithms!
Upon purchase, you will be able to Register an account to access all the algorithms on multiple devices.
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)