Created
September 15, 2019 02:41
-
-
Save Camsbury/0045fbad22cc8bdb77e0ea952e4b8cd2 to your computer and use it in GitHub Desktop.
Calculate the number of chars to remove to make two strings anagrams of each other.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns anagram-difference) | |
(defn add-to-bag [bag item] | |
(update bag item (fnil inc 0))) | |
(defn sub-from-bag [bag item] | |
(update bag item (fnil dec 0))) | |
(defn anagram-difference [w1 w2] | |
"Example | |
First word : c od e w ar s (4 letters removed) | |
Second word : ha c k er r a nk (6 letters removed) | |
Result : 10" | |
(-> {} | |
(#(reduce add-to-bag % w1)) | |
(#(reduce sub-from-bag % w2)) | |
((fn [bag] (apply + (map #(Math/abs %) (vals bag))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment