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

Testing and refactor method isOneEditDistance from interviews\leetcode\string\src\main\java #240

Open
wants to merge 3 commits 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
26 changes: 26 additions & 0 deletions leetcode/string/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>string</groupId>
<artifactId>string</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
//return 2.
//Note: You may assume the string contain only lowercase letters.

import java.util.HashMap;

class FirstUniqueCharacterInAString {
public int firstUniqChar(String s) {
HashMap<Character, Integer> characters = new HashMap<Character, Integer>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
//"()()()"
//]

import java.util.ArrayList;
import java.util.List;

class GenerateParentheses {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<String>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.HashMap;

public class LongestPalindrome {
public int longestPalindrome(String s) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// If there is no such window in S that covers all characters in T, return the empty string "".
// If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

import java.util.HashMap;

public class MinimumWindowSubstring {
public String minWindow(String s, String t) {
HashMap<Character, Integer> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public boolean isOneEditDistance(String s, String t) {
} else if(s.length() < t.length()) {
//return true if the strings would be the same if you deleted a character from string t
return s.substring(i).equals(t.substring(i + 1));

} else {
//return true if the strings would be the same if you deleted a character from string s
return t.substring(i).equals(s.substring(i + 1));
Expand Down
26 changes: 26 additions & 0 deletions leetcode/string/src/main/java/OneEditDistanceRefactor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class OneEditDistanceRefactor {
public static boolean isOneEditDistance(String s, String t) {
int m = s.length();
int n = t.length();

// If the length difference is greater than 1, can not have one edit
if (Math.abs(m - n) > 1) {
return false;
}

// Make sure string s is always shorter than or equal to string t
if (m > n) {
return isOneEditDistance(t, s);
}

for (int i = 0; i < m; i++) {
if (s.charAt(i) != t.charAt(i)) {
if (m == n) {
return s.substring(i + 1).equals(t.substring(i + 1));
}
return s.substring(i).equals(t.substring(i + 1));
}
}
return (n - m) == 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// Input is guaranteed to be within the range from 1 to 3999

import java.util.HashMap;

public class RomanToInteger {
public int romanToInt(String s) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

import java.util.Stack;

public class ValidParentheses {
public boolean isValid(String s) {
if(s.length() % 2 == 1) {
Expand Down
65 changes: 65 additions & 0 deletions leetcode/string/src/test/java/OneEditDistanceRefactorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import org.junit.Test;

import static org.junit.Assert.*;

public class OneEditDistanceRefactorTest {
OneEditDistanceRefactor oneEditDistance = new OneEditDistanceRefactor();


@Test
public void testIsOneEditDistance_whenInsertOneCharacterInFirstString() {
String str1 = "ABCD";
String str2 = "ABBD";

assertTrue(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenDeleteOneCharactersInFirstString() {
String str1 = "ABCD";
String str2 = "ABD";

assertTrue(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenAddOneCharactersInFirstString() {
String str1 = "ABDE";
String str2 = "ABCDE";

assertTrue(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenTwoStringAreEquals() {
String str1 = "ABCD";
String str2 = "ABCD";

assertFalse(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenInsertTwoCharactersInFirstString() {
String str1 = "ABCD";
String str2 = "ABEF";

assertFalse(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenDeleteTwoCharactersInFirstString() {
String str1 = "ABCD";
String str2 = "AB";

assertFalse(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenAddTwoCharactersInFirstString() {
String str1 = "ABCD";
String str2 = "ABCDEF";

assertFalse(oneEditDistance.isOneEditDistance(str1, str2));
}

}
67 changes: 67 additions & 0 deletions leetcode/string/src/test/java/OneEditDistanceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class OneEditDistanceTest {
OneEditDistance oneEditDistance = new OneEditDistance();


@Test
public void testIsOneEditDistance_whenInsertOneCharacterInFirstString() {
String str1 = "ABCD";
String str2 = "ABBD";

assertTrue(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenDeleteOneCharactersInFirstString() {
String str1 = "ABCD";
String str2 = "ABD";

assertTrue(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenAddOneCharactersInFirstString() {
String str1 = "ABDE";
String str2 = "ABCDE";

assertTrue(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenTwoStringAreEquals() {
String str1 = "ABCD";
String str2 = "ABCD";

assertFalse(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenInsertTwoCharactersInFirstString() {
String str1 = "ABCD";
String str2 = "ABEF";

assertFalse(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenDeleteTwoCharactersInFirstString() {
String str1 = "ABCD";
String str2 = "AB";

assertFalse(oneEditDistance.isOneEditDistance(str1, str2));
}

@Test
public void testIsOneEditDistance_whenAddTwoCharactersInFirstString() {
String str1 = "ABCD";
String str2 = "ABCDEF";

assertFalse(oneEditDistance.isOneEditDistance(str1, str2));
}


}
Binary file added leetcode/string/target/classes/AddBinary.class
Binary file not shown.
Binary file added leetcode/string/target/classes/CountAndSay.class
Binary file not shown.
Binary file added leetcode/string/target/classes/DecodeWays.class
Binary file not shown.
Binary file added leetcode/string/target/classes/EditDistance.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added leetcode/string/target/classes/OneEditDistance.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added leetcode/string/target/classes/ValidPalindrome.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading