DEV Community

loading...

Discussion on: Daily Challenge #304 - Consecutive Letters

Collapse
cipharius profile image
Valts Liepiņš

Solution in Haskell with O(n) time complexity:

import Data.Char (ord)
import qualified Data.IntMap.Strict as IMap

solve :: String -> Bool
solve = isUniqConsecutive . toCharMap
  where
    toCharMap = IMap.fromListWith (+) . map (flip (,) 1 . ord)
    isUniqConsecutive map
      = isUniq (IMap.elems map)
      && isConsecutive (IMap.keys map)
    isUniq = (== 1) . maximum
    isConsecutive [_] = True
    isConsecutive xs  = and $ zipWith (==) xs (pred <$> tail xs)
Enter fullscreen mode Exit fullscreen mode