Skip to content

Instantly share code, notes, and snippets.

@jergason
Created October 20, 2014 16:54
Show Gist options
  • Select an option

  • Save jergason/6b1f52dbaadb53a69c91 to your computer and use it in GitHub Desktop.

Select an option

Save jergason/6b1f52dbaadb53a69c91 to your computer and use it in GitHub Desktop.
horribly awful clojure code
(defn decrement-char-count [char-map character]
(let [new-char-map (assoc char-map character (dec (get char-map character 0)))]
[(new-char-map character) new-char-map]))
(defn make-char-map [string]
(reduce (fn [char-map character] (assoc char-map character (inc (get char-map character 0)))) {} (seq string)))
(defn permutation? [str1 str2]
"return true if str2 can be formed by some permutation of str1, false otherwise"
(let [str1-map (make-char-map str1)]
(loop [characters (seq str2)
char-map str1-map]
(if (not (seq characters))
true
(let [[char-count new-char-map] (decrement-char-count char-map (first characters))]
(if (< 0 char-count)
false
(recur (rest characters) new-char-map)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment