Skip to content

Instantly share code, notes, and snippets.

@Camsbury
Created September 15, 2019 02:41
Show Gist options
  • Save Camsbury/0045fbad22cc8bdb77e0ea952e4b8cd2 to your computer and use it in GitHub Desktop.
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.
(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