-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunix_path_simplifier.hs
18 lines (15 loc) · 1012 Bytes
/
unix_path_simplifier.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
--https://leetcode.com/problems/simplify-path/
import Data.List.Split
import Data.List
simplifyPath :: String -> String
simplifyPath path | length pathList == 1 = "/"
| otherwise = intercalate "/" pathList
where
split = splitOn "/" path
pathList = reverse $ simplifyPath' split []
simplifyPath' :: [String] -> [String] -> [String]
simplifyPath' [] pathAcc = pathAcc
simplifyPath' (p : pathSplit) pathAcc | null p && null pathAcc = simplifyPath' pathSplit ("" : pathAcc)
| p == "." || null p || (length pathAcc == 1 && p == "..") = simplifyPath' pathSplit pathAcc
| p == ".." && length pathAcc > 1 = simplifyPath' pathSplit (tail pathAcc)
| otherwise = simplifyPath' pathSplit (p : pathAcc)