forked from careercup/CtCI-6th-Edition-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_5.js
35 lines (33 loc) · 995 Bytes
/
1_5.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
module.exports = Strings_1_5 = (function() {
return {
// Checks if a string can converted to another string with a single edit
// Solution #5 from the book.
// @param {String} first - first string
// @param {String} second - second string
// @retuns {Boolean} - true if a string can converted to another string with a single edit
oneAway: function(first, second) {
var skip = true;
var s1 = first.length > second.length ? first : second;
var s2 = first.length > second.length ? second : first;
var diff = s1.length - s2.length;
var i1 = 0,
i2 = 0;
if (diff > 1) {
return false;
}
while (i1 < s2.length) {
var i2 = i1 + (!skip ? diff : 0);
if (s1.charCodeAt(i2) !== s2.charCodeAt(i1)) {
if (skip) {
i1 = i1 - diff;
skip = false;
} else {
return false;
}
}
i1++;
}
return true;
}
};
}());