Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit daa223a

Browse files
Merge pull request ignacio-chiazzo#13 from ignacio-chiazzo/deletionDistance
Deletion Distance
2 parents f22a4f5 + 7a04fec commit daa223a

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

LeetcodeProblems/deletionDistance.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
Deletion Distance
3+
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:
4+
5+
By deleting 'e' and 'a' in "heat", and 'i' in "hit", we get the string "ht" in both cases.
6+
We cannot get the same string from both strings by deleting 2 letters or fewer.
7+
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.
8+
9+
input: str1 = "dog", str2 = "frog"
10+
output: 3
11+
12+
input: str1 = "some", str2 = "some"
13+
output: 0
14+
15+
input: str1 = "some", str2 = "thing"
16+
output: 9
17+
18+
input: str1 = "", str2 = ""
19+
output: 0
20+
*/
21+
22+
23+
// Solution 3 Using DP
24+
var deletionDistanceDP = function(str1, str2) {
25+
if(str1.length === 0)
26+
return str2.length;
27+
if(str2.length === 0)
28+
return str1.length;
29+
30+
var matrix = [];
31+
for(var i = 0; i <= str1.length; i++) {
32+
matrix[i] = [];
33+
for(var j = 0; j <= str2.length; j++) {
34+
if(i === 0) {
35+
matrix[i][j] = j;
36+
} else if(j == 0) {
37+
matrix[i][j] = i;
38+
} else if(str1[i - 1] === str2[j - 1]) {
39+
matrix[i][j] = matrix[i - 1][j - 1];
40+
} else {
41+
matrix[i][j] = 1 + min(matrix[i - 1][j], matrix[i][j - 1]);
42+
}
43+
}
44+
}
45+
46+
return matrix[str1.length][str2.length];
47+
}
48+
49+
// Solution 2 Using memoization
50+
var deletionDistance2 = function(str1, str2) {
51+
var memo = {};
52+
return deletionDistanceAux2(str1, str2, 0, 0, memo);
53+
}
54+
55+
var deletionDistanceAux2 = function(str1, str2, pos1, pos2, memo) {
56+
const valueCashed = getValue(pos1, pos2, memo);
57+
if(valueCashed !== undefined) {
58+
return valueCashed;
59+
}
60+
var result;
61+
62+
if(str1.length === pos1)
63+
result = str2.length - pos2;
64+
else if(str2.length === pos2)
65+
result = str1.length - pos1;
66+
else if(str1[pos1] === str2[pos2])
67+
result = deletionDistanceAux2(str1, str2, pos1 + 1, pos2 + 1, memo);
68+
else
69+
result = 1 + min(deletionDistanceAux2(str1, str2, pos1, pos2 + 1, memo), deletionDistanceAux2(str1, str2, pos1 + 1, pos2, memo))
70+
71+
return setValue(pos1, pos2, result, memo);
72+
}
73+
74+
var getMemoKey = function(pos1, pos2) {
75+
return pos1 + "-" + pos2;
76+
}
77+
78+
var getValue = function(pos1, pos2, memo) {
79+
const memoKey = getMemoKey(pos1, pos2);
80+
return memo[memoKey];
81+
}
82+
83+
var setValue = function(pos1, pos2, value, memo) {
84+
const memoKey = getMemoKey(pos1, pos2);
85+
memo[memoKey] = value;
86+
return value;
87+
}
88+
89+
// Solution 1 naive
90+
var deletionDistance = function(str1, str2) {
91+
return deletionDistanceAux(str1, str2, 0, 0);
92+
}
93+
94+
var deletionDistanceAux = function(str1, str2, pos1, pos2) {
95+
if(str1.length === pos1)
96+
return str2.length - pos2;
97+
98+
if(str2.length === pos2)
99+
return str1.length - pos1;
100+
101+
if(str1[pos1] === str2[pos2])
102+
return deletionDistanceAux(str1, str2, pos1 + 1, pos2 + 1);
103+
104+
return 1 + min(deletionDistanceAux(str1, str2, pos1, pos2 + 1), deletionDistanceAux(str1, str2, pos1 + 1, pos2));
105+
}
106+
107+
var min = function(a, b) {
108+
return (a < b) ? a : b;
109+
}
110+
111+
function main() {
112+
console.log(deletionDistance("dog", "frog")); //output: 3
113+
console.log(deletionDistance("some", "some")); //output: 0
114+
console.log(deletionDistance("some", "thing")); //output: 9
115+
console.log(deletionDistance("", "")); // = 0
116+
117+
console.log(deletionDistance2("dog", "frog")); //output: 3
118+
console.log(deletionDistance2("some", "some")); //output: 0
119+
console.log(deletionDistance2("some", "thing")); //output: 9
120+
console.log(deletionDistance2("", "")); // = 0
121+
122+
console.log(deletionDistanceDP("dog", "frog")); //output: 3
123+
console.log(deletionDistanceDP("some", "some")); //output: 0
124+
console.log(deletionDistanceDP("some", "thing")); //output: 9
125+
console.log(deletionDistanceDP("", "")); // = 0
126+
}
127+
128+
main();
129+
130+
module.exports.main = main

Main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ var loadProblems = () => {
3030
});
3131
}
3232

33-
main();
33+
main();

0 commit comments

Comments
 (0)