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

adding java classes with modified code smell & jUnit testing classes #219

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
39 changes: 39 additions & 0 deletions src/main/java/DecodeWays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// A message containing letters from A-Z is being encoded to numbers using the following mapping:

// 'A' -> 1
// 'B' -> 2
// ...
// 'Z' -> 26

// Given an encoded message containing digits, determine the total number of ways to decode it.

// For example,
// Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

// The number of ways decoding "12" is 2.

package src.main.java;

public class DecodeWays {
public int numDecodings(String s) {
int n = s.length();

if(n == 0) {
return 0;
}

int[] dp = new int[n + 1];
dp[n] = 1;
dp[n - 1] = s.charAt(n - 1) != '0' ? 1 : 0;

for(int i = n - 2; i >= 0; i--) {
if(s.charAt(i) == '0') {
continue;
} else {
dp[i] = (Integer.parseInt(s.substring(i, i + 2)) <= 26) ? dp[i + 1] + dp[i + 2] : dp[i + 1];
}
}

return dp[0];
}
}
42 changes: 42 additions & 0 deletions src/main/java/EditDistance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

// You have the following 3 operations permitted on a word:

// a) Insert a character
// b) Delete a character
// c) Replace a character

package src.main.java;
public class EditDistance {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();

int[][] dp = new int[m + 1][n + 1];

for(int i = 0; i <= m; i++) {
dp[i][0] = i;
}

for(int i = 0; i <= n; i++) {
dp[0][i] = i;
}

for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(word1.charAt(i) == word2.charAt(j)) {
dp[i + 1][j + 1] = dp[i][j];
} else {
int a = dp[i][j];
int b = dp[i][j + 1];
int c = dp[i + 1][j];

dp[i + 1][j + 1] = Math.min(a, Math.min(b, c));
dp[i + 1][j + 1]++;
}
}
}

return dp[m][n];
}
}
43 changes: 43 additions & 0 deletions src/main/java/FirstUniqueCharacterInAString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

package src.main.java;
import java.util.HashMap;

//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
//
//Examples:
//
//s = "leetcode"
//return 0.
//
//s = "loveleetcode",
//return 2.
//Note: You may assume the string contain only lowercase letters.

public class FirstUniqueCharacterInAString {
public int firstUniqChar(String s) {

//instead of assuming, always convert the characters into lowercase letters.
s = s.toLowerCase();

HashMap<Character, Integer> characters = new HashMap<Character, Integer>();

for(int i = 0; i < s.length(); i++) {
char current = s.charAt(i);
if(characters.containsKey(current)) {
characters.put(current, -1);
} else {
characters.put(current, i);
}
}

int min = Integer.MAX_VALUE;
for(char c: characters.keySet()) {
if(characters.get(c) > -1 && characters.get(c) < min) {
min = characters.get(c);
}
}

return min == Integer.MAX_VALUE ? -1 : min;

}
}
17 changes: 17 additions & 0 deletions src/main/java/ReverseWordsInAString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//Given an input string, reverse the string word by word.
//For example,
//Given s = "the sky is blue",
//return "blue is sky the".

package src.main.java;
public class ReverseWordsInAString {
public String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
String result = "";
for(int i = words.length - 1; i > 0; i--) {
result += words[i] + " ";
}

return result + words[0];
}
}
38 changes: 38 additions & 0 deletions src/main/java/RomanToInteger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package src.main.java;
import java.util.HashMap;

// Given a roman numeral, convert it to an integer.

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

public class RomanToInteger {
public int romanToInt(String s) {
//instead of assuming, always convert the roman numerals into uppercase letters.
//to avoid unexpected errors as roman numerals are accepted input as lowercased too
s = s.toUpperCase();

HashMap<Character, Integer> map = new HashMap<Character, Integer>();

map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);

int total = 0;

for(int i = 0; i < s.length() - 1; i++) {
if(map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
total -= map.get(s.charAt(i));
} else {
total += map.get(s.charAt(i));
}
}

total += map.get(s.charAt(s.length() - 1));

return total;
}
}
66 changes: 66 additions & 0 deletions src/main/java/SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package src.main.java;

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

//Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
//
//Example 1:
//
//Input:
//[
//[ 1, 2, 3 ],
//[ 4, 5, 6 ],
//[ 7, 8, 9 ]
//]
//Output: [1,2,3,6,9,8,7,4,5]
//Example 2:
//
//Input:
//[
//[1, 2, 3, 4],
//[5, 6, 7, 8],
//[9,10,11,12]
//]
//Output: [1,2,3,4,8,12,11,10,9,5,6,7]

public class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<Integer>();
if(matrix == null || matrix.length == 0) {
return result;
}

int rowStart = 0;
int rowEnd = matrix.length - 1;
int colStart = 0;
int colEnd = matrix[0].length - 1;
while(rowStart <= rowEnd && colStart <= colEnd) {
for(int i = colStart; i <= colEnd; i++) {
result.add(matrix[rowStart][i]);
}
rowStart++;

for(int i = rowStart; i <= rowEnd; i++) {
result.add(matrix[i][colEnd]);
}
colEnd--;

if(rowStart <= rowEnd) {
for(int i = colEnd; i >= colStart; i--) {
result.add(matrix[rowEnd][i]);
}
}
rowEnd--;

if(colStart <= colEnd) {
for(int i = rowEnd; i >= rowStart; i--) {
result.add(matrix[i][colStart]);
}
}
colStart++;
}

return result;
}
}
42 changes: 42 additions & 0 deletions src/main/java/jUnit/DecodeWaysTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package src.main.java.jUnit;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

import src.main.java.DecodeWays;


//A message containing letters from A-Z is being encoded to numbers using the following mapping:

//'A' -> 1
//'B' -> 2
//...
//'Z' -> 26

//Given an encoded message containing digits, determine the total number of ways to decode it.

//For example,
//Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

//The number of ways decoding "12" is 2.

class DecodeWaysTest {

DecodeWays testDW = new DecodeWays();

/* ways of decoding message match the function's result */
@Test
void testMatch() {
assertEquals(3, testDW.numDecodings("123"));
//123 = A,B,C | AB,C | A,BC
}


/* ways of decoding message don't match the function's result */
@Test
void testNoMatch() {
assertEquals(12, testDW.numDecodings("123"));
//123 = A,B,C | AB,C | A,BC
}
}
43 changes: 43 additions & 0 deletions src/main/java/jUnit/EditDistanceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package src.main.java.jUnit;

import static org.junit.jupiter.api.Assertions.*;

//Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2.
// (each operation - add, delete, replace is counted as 1 step.)

import org.junit.jupiter.api.Test;

import src.main.java.EditDistance;

class EditDistanceTest {

EditDistance test = new EditDistance();

/* only requires add 1 letter */
@Test
void testOneChar() {
assertEquals(1, test.minDistance("a", "ab"));
}

/* requires add 2 letters */
@Test
void testTwoChar() {
assertEquals(2, test.minDistance("a", "abc"));
//each character is counted as 1 operation.
}

/* requires add 2 letters and delete 1 letter */
@Test
void testAddReplace() {
assertEquals(2, test.minDistance("a", "bc"));
//b is not a, so a is replaced by b, therefore, there are only 2 steps: replace and add.
}

/* requires add 2 letters and delete 2 letters */
@Test
void testAddDelete() {
assertEquals(2, test.minDistance("de", "bc"));
//can also be understood as replace 2 characters
}

}
49 changes: 49 additions & 0 deletions src/main/java/jUnit/FirstUniqueCharacterInAStringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package src.main.java.jUnit;

import static org.junit.jupiter.api.Assertions.*;

//Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
//
//Examples:
//
//s = "leetcode"
//return 0.
//
//s = "loveleetcode",
//return 2.


import org.junit.jupiter.api.Test;

import src.main.java.FirstUniqueCharacterInAString;

class FirstUniqueCharacterInAStringTest {

FirstUniqueCharacterInAString testFUC = new FirstUniqueCharacterInAString();

/* the second letter unique */
@Test
void testSecond() {
assertEquals(1, testFUC.firstUniqChar("sos"));
}

/* the first and second letters are unique */
@Test
void testTwoUnique() {
assertEquals(0, testFUC.firstUniqChar("so"));
}

/* no letters are unique */
@Test
void testNo() {
assertEquals(-1, testFUC.firstUniqChar("soos"));
}

/* the first non-unique letter is being cased different from its duplicate*/
@Test
void testUpper() {
assertEquals(1, testFUC.firstUniqChar("saS"));
//fix code smell in order to have correct result
}

}
Loading