-
Notifications
You must be signed in to change notification settings - Fork 90
/
Deletion_Distance.js
115 lines (93 loc) · 3.24 KB
/
Deletion_Distance.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Deletion Distance
The deletion distance of two strings is the minimum number of characters you need to delete in the two strings in order to get the same string. For instance, the deletion distance between "heat" and "hit" is 3:
By deleting 'e' and 'a' in "heat", and 'i' in "hit", we get the string "ht" in both cases.
We cannot get the same string from both strings by deleting 2 letters or fewer.
Given the strings str1 and str2, write an efficient function deletionDistance that returns the deletion distance between them. Explain how your function works, and analyze its time and space complexities.
input: str1 = "dog", str2 = "frog"
output: 3
input: str1 = "some", str2 = "some"
output: 0
input: str1 = "some", str2 = "thing"
output: 9
input: str1 = "", str2 = ""
output: 0
*/
// Solution 3 Using DP
var deletionDistanceDP = function (str1, str2) {
if (str1.length === 0) return str2.length;
if (str2.length === 0) return str1.length;
var matrix = [];
for (var i = 0; i <= str1.length; i++) {
matrix[i] = [];
for (var j = 0; j <= str2.length; j++) {
if (i === 0) {
matrix[i][j] = j;
} else if (j == 0) {
matrix[i][j] = i;
} else if (str1[i - 1] === str2[j - 1]) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = 1 + min(matrix[i - 1][j], matrix[i][j - 1]);
}
}
}
return matrix[str1.length][str2.length];
};
// Solution 2 Using memoization
var deletionDistance2 = function (str1, str2) {
var memo = {};
return deletionDistanceAux2(str1, str2, 0, 0, memo);
};
var deletionDistanceAux2 = function (str1, str2, pos1, pos2, memo) {
const valueCashed = getValue(pos1, pos2, memo);
if (valueCashed !== undefined) return valueCashed;
var result;
if (str1.length === pos1) result = str2.length - pos2;
else if (str2.length === pos2) result = str1.length - pos1;
else if (str1[pos1] === str2[pos2])
result = deletionDistanceAux2(str1, str2, pos1 + 1, pos2 + 1, memo);
else
result =
1 +
min(
deletionDistanceAux2(str1, str2, pos1, pos2 + 1, memo),
deletionDistanceAux2(str1, str2, pos1 + 1, pos2, memo)
);
return setValue(pos1, pos2, result, memo);
};
var getMemoKey = function (pos1, pos2) {
return pos1 + "-" + pos2;
};
var getValue = function (pos1, pos2, memo) {
const memoKey = getMemoKey(pos1, pos2);
return memo[memoKey];
};
var setValue = function (pos1, pos2, value, memo) {
const memoKey = getMemoKey(pos1, pos2);
memo[memoKey] = value;
return value;
};
// Solution 1 naive
var deletionDistance = function (str1, str2) {
return deletionDistanceAux(str1, str2, 0, 0);
};
var deletionDistanceAux = function (str1, str2, pos1, pos2) {
if (str1.length === pos1) return str2.length - pos2;
if (str2.length === pos2) return str1.length - pos1;
if (str1[pos1] === str2[pos2])
return deletionDistanceAux(str1, str2, pos1 + 1, pos2 + 1);
return (
1 +
min(
deletionDistanceAux(str1, str2, pos1, pos2 + 1),
deletionDistanceAux(str1, str2, pos1 + 1, pos2)
)
);
};
var min = function (a, b) {
return a < b ? a : b;
};
module.exports.deletionDistance = deletionDistance;
module.exports.deletionDistance2 = deletionDistance2;
module.exports.deletionDistanceDP = deletionDistanceDP;