Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
39 lines (27 loc) · 1000 Bytes

File metadata and controls

39 lines (27 loc) · 1000 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class Solution {
public int minDistance(String word1, String word2) {
// Start typing your Java solution below
// DO NOT write main() function
int len1 = word1.length()+1;
int len2 = word2.length()+1;
if(len1 ==1) return len2-1;
if(len2 ==1) return len1-1;
int[][] mat = new int[len1][len2];
for(int i=0; i<len2; i++){
mat[0][i] = i;
}
for(int i=0; i<len1; i++){
mat[i][0] = i;
}
for(int i=1; i<len1; i++)
for(int j=1; j<len2; j++){
if(word1.charAt(i-1) == word2.charAt(j-1)){
mat[i][j] = mat[i-1][j-1];
}
else{
mat[i][j] = Math.min(mat[i-1][j-1],Math.min(mat[i-1][j], mat[i][j-1]))+1;
}
}
return mat[len1-1][len2-1];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.