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

Commit cc50e88

Browse files
author
cd155
committed
add 1.5 one edit away
1 parent 2457d72 commit cc50e88

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Array.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,33 @@ isPermPalin xs
100100
| otherwise = False
101101
where values = elems (convToDict xs empty)
102102
validValues = filter odd values
103+
104+
{-
105+
1.5
106+
There are three types of edits that can be performed on
107+
strings: insert a character, remove a character, or replace
108+
a character. Given two strings, write a function to check if
109+
they are one edit (or zero edits) away.
110+
111+
Test Case:
112+
isOneEditAway "pale" "bale" -> True
113+
isOneEditAway "pale" "bake" -> False
114+
isOneEditAway "pales" "pake" -> False
115+
isOneEditAway "pales" "pale" -> True
116+
isOneEditAway "pale" "ple" -> True
117+
-}
118+
isOneEditAway :: String -> String -> Bool
119+
isOneEditAway xs ys
120+
| any (\x -> x>1 || x<(-1)) ysValues = False
121+
| length oneEdits > 2 = False
122+
| sum ysValues == 0 || sum ysValues == 1 || sum ysValues == -1 = True
123+
| otherwise = False
124+
where ysValues = elems $ updateYsDict xs (convToDict ys empty)
125+
oneEdits = filter (\x -> x==1 || x==(-1)) ysValues
126+
127+
-- update ysDict base on xs
128+
updateYsDict :: String -> Map Char Integer -> Map Char Integer
129+
updateYsDict [] ysDict = ysDict
130+
updateYsDict (x:xs) ysDict
131+
| x `member` ysDict = updateYsDict xs (adjust (1-) x ysDict)
132+
| otherwise = updateYsDict xs (insert x (-1) ysDict)

0 commit comments

Comments
 (0)