11module Array where
22import Data.Map (Map , insert , member , adjust , empty , elems )
3- import Data.Char (ord )
3+ import Data.Char (ord , intToDigit )
44
55{-
66 1.1
@@ -116,7 +116,7 @@ isPermPalin xs
116116 isOneEditAway "pale" "ple" -> True
117117-}
118118isOneEditAway :: String -> String -> Bool
119- isOneEditAway xs ys
119+ isOneEditAway xs ys
120120 | any (\ x -> x> 1 || x< (- 1 )) ysValues = False
121121 | length oneEdits > 2 = False
122122 | sum ysValues == 0 || sum ysValues == 1 || sum ysValues == - 1 = True
@@ -126,7 +126,30 @@ isOneEditAway xs ys
126126
127127-- update ysDict base on xs
128128updateYsDict :: String -> Map Char Integer -> Map Char Integer
129- updateYsDict [] ysDict = ysDict
130- updateYsDict (x: xs) ysDict
129+ updateYsDict [] ysDict = ysDict
130+ updateYsDict (x: xs) ysDict
131131 | x `member` ysDict = updateYsDict xs (adjust (1 - ) x ysDict)
132132 | otherwise = updateYsDict xs (insert x (- 1 ) ysDict)
133+
134+ {-
135+ 1.6
136+ Implement a method to perform basic string compression
137+ using the counts of repeated characters. For example,
138+ the string aabcccccaaa would become a2blc5a3. If the
139+ "compressed" string would not become smaller than the
140+ original string, your method should return the original
141+ string. You can assume the string has only uppercase and
142+ lowercase letters (a - z).
143+
144+ Test Case:
145+ compreString "aabcccccaaa" -> "a2b1c5a3"
146+ -}
147+ compreString :: String -> String
148+ compreString xs = compreStrHelper xs " "
149+
150+ compreStrHelper :: String -> String -> String
151+ compreStrHelper [] holder = head holder: show (length holder)
152+ compreStrHelper (x: xs) holder
153+ | null holder = compreStrHelper xs [x]
154+ | head holder == x = compreStrHelper xs (x: holder)
155+ | otherwise = head holder: show (length holder) ++ compreStrHelper xs [x]
0 commit comments