-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoneAway.js
60 lines (53 loc) · 1.87 KB
/
oneAway.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// One Away: 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.
function oneAway(first, second) {
if (first.length - second.length > 1) {
console.log("false");
return false;
}
const firstArray = first.split("");
const secondArray = second.split("");
if (firstArray.length > secondArray.length) {
console.log("first string is longer than second string");
var j;
for (j = 0; j < firstArray.length - 1; j++) {
if (
firstArray[j] != secondArray[j] &&
firstArray[j] != secondArray[j + 1]
) {
firstArray.splice(j, 1);
console.log(firstArray.join("") == secondArray.join(""));
return firstArray.join("") == secondArray.join("");
}
}
} else if (secondArray.length > firstArray.length) {
var k;
for (k = 0; k < secondArray.length - 1; k++) {
if (
secondArray[k] != firstArray[k] &&
secondArray[k] != firstArray[k + 1]
) {
secondArray.splice(k, 1);
console.log(firstArray.join("") == secondArray.join(""));
return firstArray.join("") == secondArray.join("");
}
}
} else if (first.length == second.length) {
console.log("first string is the same length as second string");
var i;
for (i = 0; i < firstArray.length - 1; i++) {
if (firstArray[i] != secondArray[i]) {
firstArray[i] = secondArray[i];
console.log(firstArray.join("") == secondArray.join(""));
return firstArray.join("") == secondArray.join("");
}
}
}
console.log(firstArray.join("") == secondArray.join(""));
return firstArray.join("") == secondArray.join("");
}
oneAway("pale", "ple");
oneAway("pales", "pale");
oneAway("pale", "bale");
oneAway("pale", "bake");