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

Commit bab256a

Browse files
author
cd155
committed
add 1.6 string compression
1 parent cc50e88 commit bab256a

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/Array.hs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Array where
22
import 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
-}
118118
isOneEditAway :: 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
128128
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
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

Comments
 (0)