Skip to content

Commit 8956b5d

Browse files
add 583
1 parent 22cbe0d commit 8956b5d

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/)|[Solution](../master/src/main/java/com/stevesun/solutions/_583.java) | O(m*n) |O(m*n) could be optimized to O(n) | Medium | DP
2324
|582|[Kill Process](https://leetcode.com/problems/kill-process/)|[Solution](../master/src/main/java/com/stevesun/solutions/_582.java) | O(n) |O(h) | Medium | Stack
2425
|581|[Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/)|[Solution](../master/src/main/java/com/stevesun/solutions/_581.java) | O(n) |O(1) | Easy | Array, Sort
2526
|575|[Distribute Candies](https://leetcode.com/problems/distribute-candies/)|[Solution](../master/src/main/java/com/stevesun/solutions/_575.java) | O(nlogn) |O(1) | Easy | Array
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.stevesun.solutions;
2+
3+
/**
4+
* 583. Delete Operation for Two Strings
5+
*
6+
* Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.
7+
8+
Example 1:
9+
Input: "sea", "eat"
10+
Output: 2
11+
12+
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
13+
14+
Note:
15+
The length of given words won't exceed 500.
16+
Characters in given words can only be lower-case letters.
17+
*/
18+
public class _583 {
19+
20+
public int minDistance(String word1, String word2) {
21+
int m = word1.length();
22+
int n = word2.length();
23+
int[][] dp = new int[m+1][n+1];
24+
for (int i = 1; i <= m; i++) {
25+
for (int j = 1; j <= n; j++) {
26+
dp[i][j] = word1.charAt(i-1) == word2.charAt(j-1) ? dp[i-1][j-1]+1 : Math.max(dp[i-1][j], dp[i][j-1]);
27+
}
28+
}
29+
return m + n - 2*dp[m][n];
30+
}
31+
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.stevesun;
2+
3+
import com.stevesun.solutions._48;
4+
import com.stevesun.solutions._583;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Created by stevesun on 5/18/17.
12+
*/
13+
public class _583Test {
14+
private static _583 test;
15+
private static String word1;
16+
private static String word2;
17+
18+
@BeforeClass
19+
public static void setup(){
20+
test = new _583();
21+
}
22+
23+
@Test
24+
public void test1(){
25+
word1 = "sea";
26+
word2 = "eat";
27+
assertEquals(2, test.minDistance(word1, word2));
28+
}
29+
30+
@Test
31+
public void test2(){
32+
word1 = "sea";
33+
word2 = "ate";
34+
assertEquals(4, test.minDistance(word1, word2));
35+
}
36+
}

0 commit comments

Comments
 (0)