forked from Turingfly/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneAway.java
103 lines (98 loc) · 2.42 KB
/
OneAway.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package chapter01ArraysAndStrings;
/**
*
* Problem: One Away: There are three types of edits that can be performed on
* strings: insert a character, remove a character, or replace a character.
* Given two strings, write a function to check if they are one edit (or zero
* edits) away.
*
* Solution:
*
*/
public class OneAway {
/**
* method1: replace, insert, delete.It is clearer and easier to follow but
* has some duplicate code.
*/
public boolean oneEditAway(String s1, String s2) {
if (s1 == null || s2 == null) {
return false;
}
if (s1.length() == s2.length()) {
return replace(s1, s2);
} else if (s1.length() + 1 == s2.length()) {
return insert(s1, s2);
} else if (s1.length() - 1 == s2.length()) {
return insert(s2, s1);
}
return false;
}
private boolean replace(String s1, String s2) {
boolean foundDiff = false;
char[] chars1 = s1.toCharArray();
char[] chars2 = s2.toCharArray();
for (int i = 0; i < chars1.length; i++) {
if (chars1[i] != chars2[2]) {
if (foundDiff) {
return false;
}
foundDiff = true;
}
}
return true;
}
private boolean insert(String s1, String s2) {
int index1 = 0;
int index2 = 0;
char[] chars1 = s1.toCharArray();
char[] chars2 = s2.toCharArray();
while (index1 < s1.length() && index2 < s2.length()) {
if (chars1[index1] != chars2[index2]) {
if (index1 != index2) {
return false;
}
index2++;
} else {
index1++;
index2++;
}
}
return true;
}
/**
* method2: Handle replace and insert in the same method;
*/
public boolean method2(String s1, String s2) {
if (s1 == null || s2 == null || s1.length() - s2.length() == 0) {
return false;
}
String str1 = s1.length() < s2.length() ? s1 : s2;
String str2 = s1.length() < s2.length() ? s2 : s1;
assert (str1.length() <= str2.length());
int index1 = 0;
int index2 = 0;
char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
boolean foundDiff = false;
while (index1 < s1.length() && index2 < s2.length()) {
if (chars1[index1] != chars2[index2]) {
if (foundDiff) {
return false;
}
foundDiff = true;
if (chars1.length == chars2.length) {
index1++;
}
} else {
index1++;
}
index2++;
}
return true;
}
public static void main(String[] args) {
OneAway one = new OneAway();
System.out.println(one.oneEditAway("ac", "abc"));
System.out.println(one.method2("ac", "abc"));
}
}