module Anagram_Sol where import Data.Char import qualified Data.Map as M import Data.Maybe import Bag_Sol as B {-H12.4-} wordToBag :: String -> Bag Char wordToBag = B.fromList . map toLower . filter (/= ' ') anagram :: [String] -> String -> Maybe String anagram words phrase = listToMaybe $ map unwords $ go (wordToBag phrase) where go p | B.null p = [[]] | otherwise = [ cand : rest | cand <- words, let cand' = wordToBag cand, cand' `isSubbag` p, let p' = p `B.diff` cand', rest <- go p' ]