Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork311
create OneAwayTest.java to test methods and create OneAwayFix.java to…#46
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
lephonglam wants to merge1 commit intorampatra:masterChoose a base branch fromlephonglam:test_junit
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletionssrc/.classpath
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionssrc/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/ |
17 changes: 17 additions & 0 deletionssrc/.project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionssrc/main/java/com/ctci/arraysandstrings/OneAway.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletionssrc/main/java/com/ctci/arraysandstrings/OneAwayFix.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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 deletionssrc/main/java/com/ctci/arraysandstrings/OneAwayTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.