Determine if a string has all unique characters: Difference between revisions

m
Line 530:
(zip [0 ..] xs))))
 
-- OR, fusing filter, toList, and minimumByMay down to a single fold:
duplicatedCharIndices_ :: String -> Maybe (Char, [Int])
duplicatedCharIndices_ xs =
in M.foldrWithKey
let go k [_] mb = mb -- Unique.
go
go k xs Nothing = Just (k, xs) -- Duplicate.
Nothing
go k xs@(x:_) (Just (c, ys@(y:_))) =
(foldl' if x < y--'
(\a (i, c) then-> JustM.insert c (k,maybe xs)[i] --(++ Earlier[i]) duplicate(M.lookup c a)) a)
else Just (c, ys)M.empty
(zip [0 ..] xs))
in M.foldrWithKey
gowhere
go k [_] Nothingmb = mb -- Unique.
go k xs Nothing = Just (foldl'k, xs) --' Duplicate.
go k xs@(\a (i, cx:_) -> M.insert c (maybe [i]Just (++ [i])c, ys@(M.lookup c ay:_))) a)=
if x < M.emptyy
then Just (zipk, [0xs) -- Earlier duplicate..] xs))
go k xs@(x:_)else (Just (c, ys@(y:_))) =
 
 
---------------------------TEST----------------------------
Line 564 ⟶ 566:
duplicatedCharIndices_
["", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]
 
 
--------------------------DISPLAY--------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
in unlines $
let rjust n c = drop . length <*> (replicate n c ++)
s : fmap (((++) w. =rjust maximumw (length' ' . xShow) <$*> xs((" -> " ++) . fxShow . f)) xs
where
in unlines $
rjust sn :c fmap= (((++)drop . rjust w ' ' . xShow)length <*> (("replicate ->n "c ++) . fxShow . f)) xs</lang>
w = maximum (length . xShow <$> xs)</lang>
{{Out}}
<pre>First duplicated character, if any:
9,659

edits