Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create OneAwayTest.java to test methods and create OneAwayFix.java to… #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions src/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>src</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
6 changes: 3 additions & 3 deletions src/main/java/com/ctci/arraysandstrings/OneAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class OneAway {
* @param s2
* @return
*/
private static boolean isOneEditAway(String s1, String s2) {
public static boolean isOneEditAway(String s1, String s2) {
if (s1.length() == s2.length()) {
return isOneCharacterDiffAtMax(s1, s2);
} else if (s1.length() < s2.length()) {
Expand All @@ -24,7 +24,7 @@ private static boolean isOneEditAway(String s1, String s2) {
}
}

private static boolean isOneCharacterDiffAtMax(String s1, String s2) {
public static boolean isOneCharacterDiffAtMax(String s1, String s2) {
boolean foundDiff = false;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) {
Expand All @@ -37,7 +37,7 @@ private static boolean isOneCharacterDiffAtMax(String s1, String s2) {
return true;
}

private static boolean checkForMaxOneInsertOrDeleteInS1(String s1, String s2) {
public static boolean checkForMaxOneInsertOrDeleteInS1(String s1, String s2) {
int i = 0;
int j = 0;
int s1Len = s1.length();
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/ctci/arraysandstrings/OneAwayFix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.ctci.arraysandstrings;

/**
* @author rampatra
* @since 24/11/2018
*/
public class OneAwayFix {

/**
* Checks if two strings are only one edit away, that is, by inserting, deleting, or editing
* at max one character in {@code s1} it becomes same as {@code s2}.
*
* @param s1
* @param s2
* @return
*/
public static boolean isOneEditAway(String s1, String s2) {
if(s1 == null || s2 == null) {
throw new NullPointerException("s1 or s2 parameter is null");
}
if (s1.length() == s2.length()) {
return isOneCharacterDiffAtMax(s1, s2);
} else {
return checkForMaxOneInsertOrDeleteInS1(s1, s2);
}
}

public static boolean isOneCharacterDiffAtMax(String s1, String s2) {
boolean foundDiff = false;
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) {
if (foundDiff) {
return false; // means we already found a difference earlier
}
foundDiff = true;
}
}
return true;
}

public static boolean checkForMaxOneInsertOrDeleteInS1(String s1, String s2) {
int i = 0;
int j = 0;
int s1Len = s1.length();
int s2Len = s2.length();
if (Math.abs(s1Len - s2Len) > 1) return false;

while (i < s1Len && j < s2Len) {
if (s1.charAt(i) != s2.charAt(j)) {
if (s1Len > s2Len) {
i++;
} else {
j++;
}
continue;
}
i++;
j++;
}
return Math.abs(i - j) <= 1; // check whether difference in two strings is not more than 1
}

public static void main(String[] args) {
System.out.println("pale, ple: " + isOneEditAway("pale", "ple"));
System.out.println("pales,pale: " + isOneEditAway("pales", "pale"));
System.out.println("pale, bale: " + isOneEditAway("pale", "bale"));
System.out.println("pale, bake: " + isOneEditAway("pale", "bake"));
System.out.println("ram, rama: " + isOneEditAway("ram", "rama"));
System.out.println("ram, ramaaaaaaa: " + isOneEditAway("ram", "ramaaaaaaa"));
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/ctci/arraysandstrings/OneAwayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.ctci.arraysandstrings;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;



class OneAwayTest {

@Test
static void testIsOneCharacterDiffAtMax() {
assertTrue(OneAway.isOneCharacterDiffAtMax("", ""));
assertTrue(OneAway.isOneCharacterDiffAtMax("abcdef", "abcdef"));
assertTrue(OneAway.isOneCharacterDiffAtMax("abc", "abd"));

assertFalse(OneAway.isOneCharacterDiffAtMax("abcdef", "abcfed"));
assertFalse(OneAway.isOneCharacterDiffAtMax("abcabc", "abcdef"));
}

@Test
static void testCheckForMaxOneInsertOrDeleteInS1() {
assertFalse(OneAway.checkForMaxOneInsertOrDeleteInS1("mn", "mnpq"));
assertFalse(OneAway.checkForMaxOneInsertOrDeleteInS1("abcd", "e"));

assertTrue(OneAway.checkForMaxOneInsertOrDeleteInS1("", "p"));
assertTrue(OneAway.checkForMaxOneInsertOrDeleteInS1("e", ""));

assertTrue(OneAway.checkForMaxOneInsertOrDeleteInS1("bubble", "buble"));
assertTrue(OneAway.checkForMaxOneInsertOrDeleteInS1("apple", "apples"));

assertFalse(OneAway.isOneEditAway("abcdef", "abcfeda"));
}

@Test
static void testIsOneEditAway() {
assertTrue(OneAway.isOneEditAway("", ""));
assertTrue(OneAway.isOneEditAway("abc", "abc"));
assertTrue(OneAway.isOneEditAway("pale", "bale"));
assertFalse(OneAway.isOneEditAway("pale", "bake"));

assertTrue(OneAway.isOneEditAway("pale", "pales"));
assertFalse(OneAway.isOneEditAway("ram", "ramaaaaaaa"));

assertTrue(OneAway.isOneEditAway("pale", "ple"));
assertFalse(OneAway.isOneEditAway("ramaaaaaaa", "ram"));

}
}