Last active
December 13, 2015 17:38
-
-
Save kachayev/4949030 to your computer and use it in GitHub Desktop.
Define function that returns count of unique substrings consuming O(1) memory
This file contains 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
import Data.List | |
uniqSubstr :: String -> Int | |
uniqSubstr = sum . (map pair) . pairwise . sort . tails | |
where | |
pairwise l@(_:ht) = zip l ht | |
prefix pre post = length $ takeWhile (uncurry (==)) $ zip pre post | |
pair (pre, post) = length $ drop (prefix pre post) post |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment