Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 846534b

Browse files
committed
create OneAwayTest.java to test methods and create OneAwayFix.java to fix isOneEditAway method
1 parent 957d03b commit 846534b

File tree

6 files changed

+157
-3
lines changed

6 files changed

+157
-3
lines changed

src/.classpath

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="main/java"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
5+
<attributes>
6+
<attribute name="module" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5">
10+
<attributes>
11+
<attribute name="module" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="output" path="bin"/>
15+
</classpath>

src/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

src/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>src</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

src/main/java/com/ctci/arraysandstrings/OneAway.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class OneAway {
1414
* @param s2
1515
* @return
1616
*/
17-
private static boolean isOneEditAway(String s1, String s2) {
17+
public static boolean isOneEditAway(String s1, String s2) {
1818
if (s1.length() == s2.length()) {
1919
return isOneCharacterDiffAtMax(s1, s2);
2020
} else if (s1.length() < s2.length()) {
@@ -24,7 +24,7 @@ private static boolean isOneEditAway(String s1, String s2) {
2424
}
2525
}
2626

27-
private static boolean isOneCharacterDiffAtMax(String s1, String s2) {
27+
public static boolean isOneCharacterDiffAtMax(String s1, String s2) {
2828
boolean foundDiff = false;
2929
for (int i = 0; i < s1.length(); i++) {
3030
if (s1.charAt(i) != s2.charAt(i)) {
@@ -37,7 +37,7 @@ private static boolean isOneCharacterDiffAtMax(String s1, String s2) {
3737
return true;
3838
}
3939

40-
private static boolean checkForMaxOneInsertOrDeleteInS1(String s1, String s2) {
40+
public static boolean checkForMaxOneInsertOrDeleteInS1(String s1, String s2) {
4141
int i = 0;
4242
int j = 0;
4343
int s1Len = s1.length();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.ctci.arraysandstrings;
2+
3+
/**
4+
* @author rampatra
5+
* @since 24/11/2018
6+
*/
7+
public class OneAwayFix {
8+
9+
/**
10+
* Checks if two strings are only one edit away, that is, by inserting, deleting, or editing
11+
* at max one character in {@code s1} it becomes same as {@code s2}.
12+
*
13+
* @param s1
14+
* @param s2
15+
* @return
16+
*/
17+
public static boolean isOneEditAway(String s1, String s2) {
18+
if(s1 == null || s2 == null) {
19+
throw new NullPointerException("s1 or s2 parameter is null");
20+
}
21+
if (s1.length() == s2.length()) {
22+
return isOneCharacterDiffAtMax(s1, s2);
23+
} else {
24+
return checkForMaxOneInsertOrDeleteInS1(s1, s2);
25+
}
26+
}
27+
28+
public static boolean isOneCharacterDiffAtMax(String s1, String s2) {
29+
boolean foundDiff = false;
30+
for (int i = 0; i < s1.length(); i++) {
31+
if (s1.charAt(i) != s2.charAt(i)) {
32+
if (foundDiff) {
33+
return false; // means we already found a difference earlier
34+
}
35+
foundDiff = true;
36+
}
37+
}
38+
return true;
39+
}
40+
41+
public static boolean checkForMaxOneInsertOrDeleteInS1(String s1, String s2) {
42+
int i = 0;
43+
int j = 0;
44+
int s1Len = s1.length();
45+
int s2Len = s2.length();
46+
if (Math.abs(s1Len - s2Len) > 1) return false;
47+
48+
while (i < s1Len && j < s2Len) {
49+
if (s1.charAt(i) != s2.charAt(j)) {
50+
if (s1Len > s2Len) {
51+
i++;
52+
} else {
53+
j++;
54+
}
55+
continue;
56+
}
57+
i++;
58+
j++;
59+
}
60+
return Math.abs(i - j) <= 1; // check whether difference in two strings is not more than 1
61+
}
62+
63+
public static void main(String[] args) {
64+
System.out.println("pale, ple: " + isOneEditAway("pale", "ple"));
65+
System.out.println("pales,pale: " + isOneEditAway("pales", "pale"));
66+
System.out.println("pale, bale: " + isOneEditAway("pale", "bale"));
67+
System.out.println("pale, bake: " + isOneEditAway("pale", "bake"));
68+
System.out.println("ram, rama: " + isOneEditAway("ram", "rama"));
69+
System.out.println("ram, ramaaaaaaa: " + isOneEditAway("ram", "ramaaaaaaa"));
70+
}
71+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.ctci.arraysandstrings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
9+
10+
class OneAwayTest {
11+
12+
@Test
13+
static void testIsOneCharacterDiffAtMax() {
14+
assertTrue(OneAway.isOneCharacterDiffAtMax("", ""));
15+
assertTrue(OneAway.isOneCharacterDiffAtMax("abcdef", "abcdef"));
16+
assertTrue(OneAway.isOneCharacterDiffAtMax("abc", "abd"));
17+
18+
assertFalse(OneAway.isOneCharacterDiffAtMax("abcdef", "abcfed"));
19+
assertFalse(OneAway.isOneCharacterDiffAtMax("abcabc", "abcdef"));
20+
}
21+
22+
@Test
23+
static void testCheckForMaxOneInsertOrDeleteInS1() {
24+
assertFalse(OneAway.checkForMaxOneInsertOrDeleteInS1("mn", "mnpq"));
25+
assertFalse(OneAway.checkForMaxOneInsertOrDeleteInS1("abcd", "e"));
26+
27+
assertTrue(OneAway.checkForMaxOneInsertOrDeleteInS1("", "p"));
28+
assertTrue(OneAway.checkForMaxOneInsertOrDeleteInS1("e", ""));
29+
30+
assertTrue(OneAway.checkForMaxOneInsertOrDeleteInS1("bubble", "buble"));
31+
assertTrue(OneAway.checkForMaxOneInsertOrDeleteInS1("apple", "apples"));
32+
33+
assertFalse(OneAway.isOneEditAway("abcdef", "abcfeda"));
34+
}
35+
36+
@Test
37+
static void testIsOneEditAway() {
38+
assertTrue(OneAway.isOneEditAway("", ""));
39+
assertTrue(OneAway.isOneEditAway("abc", "abc"));
40+
assertTrue(OneAway.isOneEditAway("pale", "bale"));
41+
assertFalse(OneAway.isOneEditAway("pale", "bake"));
42+
43+
assertTrue(OneAway.isOneEditAway("pale", "pales"));
44+
assertFalse(OneAway.isOneEditAway("ram", "ramaaaaaaa"));
45+
46+
assertTrue(OneAway.isOneEditAway("pale", "ple"));
47+
assertFalse(OneAway.isOneEditAway("ramaaaaaaa", "ram"));
48+
49+
}
50+
}

0 commit comments

Comments
 (0)