-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_1.5.js
48 lines (47 loc) · 1.19 KB
/
problem_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
36
37
38
39
40
41
42
43
44
45
46
47
48
// Problem #1.5:
// 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 oneEditAway(a, b) {
const n = a.length;
const m = b.length;
if (Math.abs(n - m) > 1) {
return false;
}
if (n === m) {
return numDiffs(a, b, n) < 2;
}
const s = `${a}${b}`;
let hash = new Map();
let v = 1;
for (let i = 0; i < n + m; i++) {
if (hash.has(s[i])) {
v = hash.get(s[i]) + 1;
}
hash.set(s[i], v);
v = 1;
}
let odds = 0;
for (let [key, value] of hash) {
if (value % 2 !== 0) {
odds++;
}
if (odds > 1) {
return false;
}
}
return true;
}
// Helper function that computes the number of differences between 2 strings of same size
function numDiffs(a, b, size) {
let diff = 0;
for (let i = 0; i < size; i++) {
if (a[i] !== b[i]) {
diff++;
}
}
return diff;
}
console.log(oneEditAway('pale', 'ple')); // true
console.log(oneEditAway('pales', 'pale')); // true
console.log(oneEditAway('pale', 'bale')); // true
console.log(oneEditAway('pale', 'bae')); // false