Tackled problem 1625
[cses-programming-tasks.git] / app / cses1622.hs
1 import Data.List (sort, inits, tails, nub)
2
3 main :: IO ()
4 main = do
5 line1 <- getLine
6 let base = sort line1
7 let perms = permutations base
8 print $ length perms
9 putStrLn $ unlines perms
10
11 permutations :: String -> [String]
12
13 permutations [] = []
14 permutations (x:[]) = [[x]]
15 permutations xs = concatMap (\(y, ys) -> map (y:) (permutations ys)) (selects xs)
16
17 selects :: String -> [(Char, String)]
18 selects [] = []
19 selects xs = nub [(head t, h ++ (tail t))
20 | (h, t) <- zip (inits xs) (tails xs)
21 , not (null t)]
22