Skip to content

Instantly share code, notes, and snippets.

@brettrowberry
Last active March 17, 2022 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettrowberry/1813d497a833b09b4e6b8f0959b8e5b1 to your computer and use it in GitHub Desktop.
Save brettrowberry/1813d497a833b09b4e6b8f0959b8e5b1 to your computer and use it in GitHub Desktop.
Function to detect isograms.
(defn isogram?
"An isogram is a word in which no letters occur more than once.
Empty string is considered an isogram in this implementation."
[^String s]
(boolean
(when (string? s)
(loop [letters (sort (seq s))]
(let [[current next & _] letters]
(cond
(nil? next) true
(= current next) false
:else (recur (rest letters))))))))
(assert
(every? isogram? [""
" "
"c"
"ca"
"cat"
"cats"]))
(assert
(not-any? isogram? [nil
"cc"
"cct"
"caa"
"catt"]))
;; TODO solve in another way or two
;; Can isograms have only 1, 2 or more?
```clojure
#(= (count (set (vals (frequencies %)))) 1 )
#(= (count %) (count (into #{} %))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment