1
1
module Array where
2
2
import Data.Map (Map , insert , member , adjust , empty , elems )
3
- import Data.Char (ord )
3
+ import Data.Char (ord , intToDigit )
4
4
5
5
{-
6
6
1.1
@@ -116,7 +116,7 @@ isPermPalin xs
116
116
isOneEditAway "pale" "ple" -> True
117
117
-}
118
118
isOneEditAway :: String -> String -> Bool
119
- isOneEditAway xs ys
119
+ isOneEditAway xs ys
120
120
| any (\ x -> x> 1 || x< (- 1 )) ysValues = False
121
121
| length oneEdits > 2 = False
122
122
| sum ysValues == 0 || sum ysValues == 1 || sum ysValues == - 1 = True
@@ -126,7 +126,30 @@ isOneEditAway xs ys
126
126
127
127
-- update ysDict base on xs
128
128
updateYsDict :: 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
131
131
| x `member` ysDict = updateYsDict xs (adjust (1 - ) x ysDict)
132
132
| 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