Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit b646ac8

Browse files
author
cd155
committed
add 1.3 replace spaces
1 parent 3f3e7bb commit b646ac8

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/Array.hs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Array where
22
import Data.Map (Map, insert, member, adjust, empty)
3+
import Data.Char (ord)
34

45
{-
56
1.1
@@ -45,11 +46,45 @@ isUniqBFHelper c (x:xs) = (c /= x) && isUniqBFHelper c xs
4546
the set with the other.
4647
-}
4748

49+
-- dictionary comparison
4850
isPerm :: String -> String -> Bool
4951
isPerm xs ys = convToDict xs empty == convToDict ys empty
5052

51-
convToDict :: String -> Map Char Int -> Map Char Int
53+
convToDict :: String -> Map Char Integer -> Map Char Integer
5254
convToDict [] dict = dict
5355
convToDict (x:xs) dict
5456
| x `member` dict = convToDict xs (adjust (1+) x dict)
5557
| 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

Comments
 (0)