Solution in Haskell with O(n) time complexity:
O(n)
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)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Solution in Haskell with
O(n)
time complexity: