-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathOneAwayLCCI.java
More file actions
50 lines (49 loc) · 1.52 KB
/
Copy pathOneAwayLCCI.java
File metadata and controls
50 lines (49 loc) · 1.52 KB
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
package findpattern;
// Source : https://leetcode.com/problems/one-away-lcci/
// Id : mst01.05
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2022/1/28
// Topic : findpattern
// Level : Medium
// Other :
// Tips :
// Links :
// Result : 100.00% 5.04%
public class OneAwayLCCI {
// 0ms
public boolean oneEditAway(String first, String second) {
if (second.length() > first.length())
return oneEditAway(second, first);
if (first.length() - second.length() > 1)
return false;
int edit = 1;
int i = 0, j = 0;
if (first.length() == second.length()) {
while (i < first.length()) {
if (first.charAt(i) != second.charAt(j)) {
edit--;
}
i++;
j++;
}
} else {
while (i < first.length()) {
// System.out.println(i+" "+j);
if (j == second.length())
return edit == 1 ? true : false;
if (first.charAt(i) == second.charAt(j)) {
i++;
j++;
} else {
if (i != first.length() - 1 && first.charAt(i + 1) == second.charAt(j)) { // 插入/删除字符继续)
edit--;
i++;
} else {
return false;
}
}
}
}
return edit < 0 ? false : true;
}
}