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
51 lines (47 loc) · 1.87 KB

File metadata and controls

51 lines (47 loc) · 1.87 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
package LeetCode;
public class MinDistance {
public MinDistance() {
// TODO Auto-generated constructor stub
}
public int minDistance(String word1, String word2) {
StringBuilder b1 = new StringBuilder(word1);
StringBuilder b2 = new StringBuilder(word2);
int change = 0;
int index = 0;
while(!b1.toString().equals(b2.toString())) {
if (index >= b1.length() || index >= b2.length()) {
int remaining = index == b1.length() ? b2.length()-index : b1.length()-index;
change = change + remaining;
break;
}
String b1String = b1.charAt(index) + "";
String b2String = b2.charAt(index) + "";
int firstB1InB2 = b2.indexOf(b1String);
//System.out.println("firstB1InB2: " + b1.charAt(index) + ": " + firstB1InB2);
int firstB2InB1 = b1.indexOf(b2String);
//System.out.println("firstB2InB1: " + b2.charAt(index) + ": " + firstB2InB1);
if((firstB1InB2 < firstB2InB1 || firstB2InB1 == -1) && firstB1InB2 != -1) {
b2 = b2.deleteCharAt(index);
change++;
} else if((firstB1InB2 > firstB2InB1 || firstB1InB2 == -1) && firstB2InB1 != -1 ){
b1 = b1.deleteCharAt(index);
change++;
} else if(firstB1InB2 == -1 && firstB2InB1 == -1){
b2 = b2.deleteCharAt(index);
change++;
} else {
index++;
}
}
return change;
}
public static void main(String[] args) {
// String a1 = "sea";
// String a2 = "eat";
String b1 = "sumonstgloy";
String b2 = "strgmongyu";
MinDistance distance = new MinDistance();
int minLength = distance.minDistance(b1, b2);
System.out.println(minLength);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.