Advertisement
Guest User

Untitled

a guest
May 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. boot.user> (defn squeeze [s c]
  2. (if-let [i (clojure.string/index-of s c)]
  3. (str (subs s 0 i) (subs s (inc i)))
  4. false))
  5. #'boot.user/squeeze
  6. boot.user> (defn scramble? [s1 s2]
  7. (loop [s1 s1 s2 (seq s2)]
  8. (if (empty? s2)
  9. true
  10. (if-let [s1-stroke (squeeze s1 (first s2))]
  11. (recur s1-stroke (rest s2))
  12. false))))
  13. #'boot.user/scramble?
  14.  
  15. boot.user> (def str2 (apply str (repeat 1000 (shuffle (range 1000)))))
  16. #'boot.user/str2
  17. boot.user> (def str1 (apply str (repeat 1000 (shuffle (range 1000)))))
  18. #'boot.user/str1
  19. boot.user> (count str2)
  20. 396000
  21. boot.user> (count str1)
  22. 391000
  23. boot.user> (time (scramble? str1 str2))
  24. "Elapsed time: 45675.783784 msecs"
  25. false
  26.  
  27. boot.user> (defn scramble?
  28. [str1 str2]
  29. (let [f2 (into (sorted-map) (frequencies str2))
  30. f1 (select-keys (into (sorted-map) (frequencies str1)) (keys f2))]
  31. (if (< (count f1) (count f2))
  32. false
  33. (every? true? (map <= (vals f2) (vals f1))))))
  34. #'boot.user/scramble?
  35.  
  36. boot.user> (time (scramble? str1 str2))
  37. "Elapsed time: 125.848442 msecs"
  38. false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement