Last active
July 18, 2018 15:53
-
-
Save asethia/d5f8704cd5ab9c2657b37086dce3e7fb to your computer and use it in GitHub Desktop.
There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalatest.{Matchers, WordSpec} | |
import scala.annotation.tailrec | |
/** | |
* There are three types of edits that can be performed on strings: insert a character, remove a character, | |
* or replace a character. | |
* Given two strings, write a function to check if they are one edit (or zero edits) away. | |
* pale, pIe -> true | |
* pales, pale -> true | |
* pale, bale -> true | |
* pale, bake -> false | |
* Time complexity - O(n+n)=> O(n) | |
* Created by Arun Sethia on 7/6/18. | |
*/ | |
trait OneAwayChar { | |
/** | |
* find oneaway using Boolean list | |
* @param in1 | |
* @param in2 | |
* @return | |
*/ | |
def oneAway1(in1: String, in2: String): Boolean = { | |
val chars: Array[Boolean] = Array.ofDim[Boolean](128) | |
in1.foreach(c => { | |
chars(c.toInt) = true | |
}) | |
@tailrec | |
def isAnyThingAway(newData: List[Char], away: List[Boolean]): Boolean = { | |
if (away.length > 1) false | |
else { | |
newData match { | |
case Nil => away.size <= 1 | |
case h :: _ => { | |
val exist = chars(h.toInt) | |
if (!exist) { | |
isAnyThingAway(newData.tail, exist :: away) | |
} else { | |
isAnyThingAway(newData.tail, away) | |
} | |
} | |
} | |
} | |
} | |
isAnyThingAway(in2.toList, Nil) | |
} | |
/** | |
* find oneaway using count away | |
* @param in1 | |
* @param in2 | |
* @return | |
*/ | |
def oneAway2(in1: String, in2: String): Boolean = { | |
val chars: Array[Boolean] = Array.ofDim[Boolean](128) | |
for (c <- in1.toList) | |
chars(c.toInt) = true | |
@tailrec | |
def isAnyThingAway(newData: List[Char], away: Int): Boolean = { | |
if (away > 1) false | |
else { | |
newData match { | |
case Nil => away <= 1 | |
case h :: _ => { | |
val exist = chars(h.toInt) | |
if (!exist) { | |
isAnyThingAway(newData.tail, away + 1) | |
} else { | |
isAnyThingAway(newData.tail, away) | |
} | |
} | |
} | |
} | |
} | |
isAnyThingAway(in2.toList, 0) | |
} | |
} | |
/** | |
* test cases for OneAwayChar functions | |
* Created by Arun Sethia on 5/21/18. | |
*/ | |
class OneAwaySpec extends WordSpec | |
with Matchers | |
with OneAwayChar { | |
"pale" should { | |
"s removed from pales" in { | |
assert(oneAway1("pales", "pale")) | |
} | |
} | |
"pales" should { | |
"added s in pale" in { | |
assert(oneAway1("pale", "pales")) | |
} | |
} | |
"pales" should { | |
"replaced p in bale" in { | |
assert(oneAway1("pale", "bale")) | |
} | |
} | |
"bake" should { | |
"added b k in pale and p l removed" in { | |
assert(!oneAway1("pale", "bake")) | |
} | |
} | |
"using away2 pale" should { | |
"s removed from pales" in { | |
assert(oneAway2("pales", "pale")) | |
} | |
} | |
"using away2 pales" should { | |
"added s in pale" in { | |
assert(oneAway2("pale", "pales")) | |
} | |
} | |
"using away2 bake" should { | |
"added b k in pale and p l removed" in { | |
assert(!oneAway2("pale", "bake")) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment