|
1 | 1 | module Array where |
2 | 2 | import Data.Map (Map, insert, member, adjust, empty) |
| 3 | +import Data.Char (ord) |
3 | 4 |
|
4 | 5 | {- |
5 | 6 | 1.1 |
@@ -45,11 +46,45 @@ isUniqBFHelper c (x:xs) = (c /= x) && isUniqBFHelper c xs |
45 | 46 | the set with the other. |
46 | 47 | -} |
47 | 48 |
|
| 49 | +-- dictionary comparison |
48 | 50 | isPerm :: String -> String -> Bool |
49 | 51 | isPerm xs ys = convToDict xs empty == convToDict ys empty |
50 | 52 |
|
51 | | -convToDict :: String -> Map Char Int -> Map Char Int |
| 53 | +convToDict :: String -> Map Char Integer -> Map Char Integer |
52 | 54 | convToDict [] dict = dict |
53 | 55 | convToDict (x:xs) dict |
54 | 56 | | x `member` dict = convToDict xs (adjust (1+) x dict) |
55 | 57 | | otherwise = convToDict xs (insert x 1 dict) |
| 58 | + |
| 59 | +-- ASCII array comparison |
| 60 | +-- initialize 128 ASCII array |
| 61 | +acciiArr = replicate 128 0 |
| 62 | + |
| 63 | +isPerm' :: String -> String -> Bool |
| 64 | +isPerm' xs ys = fillAccii xs acciiArr == fillAccii ys acciiArr |
| 65 | + |
| 66 | +fillAccii :: String -> [Integer] -> [Integer] |
| 67 | +fillAccii [] arr = arr |
| 68 | +fillAccii (x:xs) arr = |
| 69 | + fillAccii xs (init firstHalf ++ [updated] ++ secondHalf) |
| 70 | + where (firstHalf, secondHalf) = splitAt (ord x) arr |
| 71 | + updated = (arr !! (ord x - 1)) + 1 |
| 72 | + |
| 73 | +{- |
| 74 | + 1.3 |
| 75 | + Write a method to replace all spaces in a string with '%20'. |
| 76 | + You may assume that the string has sufficient space at the |
| 77 | + end to hold the additional characters,and that you are given |
| 78 | + the "true" length of the string. |
| 79 | +-} |
| 80 | +-- replace spaces with 20%, need to remove head(20%) and tail(20%) |
| 81 | +repSpace :: String -> String |
| 82 | +repSpace xs = repSpaceHelper xs False |
| 83 | + |
| 84 | +repSpaceHelper :: String -> Bool -> String |
| 85 | +repSpaceHelper [] _ = [] |
| 86 | +repSpaceHelper (x:xs) isSpaPre |
| 87 | + | x == ' ' && (not isSpaPre) = "20%" ++ repSpaceHelper xs (not isSpaPre) |
| 88 | + | x == ' ' && isSpaPre = repSpaceHelper xs isSpaPre |
| 89 | + | otherwise = x:repSpaceHelper xs False |
| 90 | + |
0 commit comments