From 91aafb207d1f74b40aad2b1cd31ac577a012ef2b Mon Sep 17 00:00:00 2001 From: viethung2 Date: Thu, 8 Jun 2023 16:26:24 +0700 Subject: [PATCH] Update test case TestCheckPermutation, TestMakingAnagram and update class CheckPermutation --- .../arraysandstrings/CheckPermutation.java | 10 +- .../TestCheckPermutation.java | 102 ++++++++++++++++++ .../strings/TestMakingAnagrams.java | 50 +++++++++ 3 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/ctci/arraysandstrings/TestCheckPermutation.java create mode 100644 src/main/java/com/hackerrank/algorithms/strings/TestMakingAnagrams.java diff --git a/src/main/java/com/ctci/arraysandstrings/CheckPermutation.java b/src/main/java/com/ctci/arraysandstrings/CheckPermutation.java index dbf7c795..9d46449d 100644 --- a/src/main/java/com/ctci/arraysandstrings/CheckPermutation.java +++ b/src/main/java/com/ctci/arraysandstrings/CheckPermutation.java @@ -15,7 +15,7 @@ public class CheckPermutation { * @param s2 * @return */ - private static boolean isOnePermutationOfOther(String s1, String s2) { + public static boolean isOnePermutationOfOther(String s1, String s2) { if (s1.length() != s2.length()) { return false; } @@ -35,7 +35,7 @@ private static boolean isOnePermutationOfOther(String s1, String s2) { * @param s2 * @return */ - private static boolean isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(String s1, String s2) { + public static boolean isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(String s1, String s2) { if (s1.length() != s2.length()) { return false; } @@ -43,10 +43,16 @@ private static boolean isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(S int[] chars = new int[128]; // assuming strings contain only ASCII characters for (int i = 0; i < s1.length(); i++) { + if(s1.charAt(i) < 0 || s1.charAt(i) >= 128) { + return false; + } chars[s1.charAt(i)]++; } for (int i = 0; i < s2.length(); i++) { + if(s2.charAt(i) < 0 || s2.charAt(i) >= 128) { + return false; + } chars[s2.charAt(i)]--; if (chars[s2.charAt(i)] < 0) { return false; diff --git a/src/main/java/com/ctci/arraysandstrings/TestCheckPermutation.java b/src/main/java/com/ctci/arraysandstrings/TestCheckPermutation.java new file mode 100644 index 00000000..7a03e673 --- /dev/null +++ b/src/main/java/com/ctci/arraysandstrings/TestCheckPermutation.java @@ -0,0 +1,102 @@ +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 TestCheckPermutation { + + @Test + void testPermutationStringWithEmpty() { + String a1 = ""; + String a2 = ""; + + String b1 = "1234567890 qwertyuiopasdfghjklzxcvbnm !@#$%^&*()-="; + String b2 = ""; + + String c1 = ""; + String c2 = "1234567890 qwertyuiopasdfghjklzxcvbnm !@#$%^&*()-="; + + + boolean resultA = CheckPermutation.isOnePermutationOfOther(a1, a2); + boolean resultB = CheckPermutation.isOnePermutationOfOther(b1, b2); + boolean resultC = CheckPermutation.isOnePermutationOfOther(c1, c2); + + assertTrue(resultA); + assertFalse(resultB); + assertFalse(resultC); + } + + @Test + void testPermutationStringAllCharacter() { + String a1 = "1234567890 qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM !@#$%^&*()-="; + String a2 = "!@#$%^&*()-= 1234567890 qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM"; + + String b1 = "~!@#$%^&*([{<>}])+-=:;'\\\",./? "; + String b2 = " ?/.,\"\\';:=-+)}]><[{(*&^%$#@!~"; + + String c1 = "Â Ă Ơ Ô Ư Ê â ă ơ ô ư ê"; + String c2 = "ê ư ô ơ ă â Ê Ư Ô Ơ Ă Â"; + + String d1 = "ÂÂ ĂĂ ƠƠ ÔÔ ƯƯ ÊÊ ââ ăă ơơ ôô ưư êê"; + String d2 = "ê ư ô ơ ă â Ê Ư Ô Ơ Ă Â"; + + + boolean resultA = CheckPermutation.isOnePermutationOfOther(a1, a2); + boolean resultB = CheckPermutation.isOnePermutationOfOther(b1, b2); + boolean resultC = CheckPermutation.isOnePermutationOfOther(c1, c2); + boolean resultD = CheckPermutation.isOnePermutationOfOther(d1, d2); + + assertTrue(resultA); + assertTrue(resultB); + assertTrue(resultC); + assertFalse(resultD); + } + + @Test + void testPermutationWithAsciiAndStringEmpty() { + String a1 = ""; + String a2 = ""; + + String b1 = "1234567890 qwertyuiopasdfghjklzxcvbnm !@#$%^&*()-="; + String b2 = ""; + + String c1 = ""; + String c2 = "1234567890 qwertyuiopasdfghjklzxcvbnm !@#$%^&*()-="; + + + boolean resultA = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(a1, a2); + boolean resultB = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(b1, b2); + boolean resultC = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(c1, c2); + + assertTrue(resultA); + assertFalse(resultB); + assertFalse(resultC); + } + + @Test + void testPermutationWithAsciiAndStringAllCharacter() { + String a1 = "1234567890 qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM !@#$%^&*()-="; + String a2 = "!@#$%^&*()-= 1234567890 qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM"; + + String b1 = "~!@#$%^&*([{<>}])+-=:;'\\\",./? "; + String b2 = " ?/.,\"\\';:=-+)}]><[{(*&^%$#@!~"; + + String c1 = "Â Ă Ơ Ô Ư Ê â ă ơ ô ư ê"; + String c2 = "ê ư ô ơ ă â Ê Ư Ô Ơ Ă Â"; + + String d1 = "Â Ă Ơ Ô Ư Ê â ă ơ ô ư ê"; + String e2 = "ê ư ô ơ ă â Ê Ư Ô Ơ Ă Â"; + + + boolean resultA = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(a1, a2); + boolean resultB = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(b1, b2); + boolean resultC = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(c1, c2); + + assertTrue(resultA); + assertTrue(resultB); + assertFalse(resultC); + } +} diff --git a/src/main/java/com/hackerrank/algorithms/strings/TestMakingAnagrams.java b/src/main/java/com/hackerrank/algorithms/strings/TestMakingAnagrams.java new file mode 100644 index 00000000..29b4ac63 --- /dev/null +++ b/src/main/java/com/hackerrank/algorithms/strings/TestMakingAnagrams.java @@ -0,0 +1,50 @@ +package com.hackerrank.algorithms.strings; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class TestMakingAnagrams { + + @Test + void testWithStringEmpty() { + String a1 = ""; + String a2 = ""; + int resultA = 0; + + int countA = MakingAnagrams.makeAnagrams(a1, a2); + assertEquals(countA, resultA); + + String b1 = ""; + String b2 = "1234567890qwertyuiop"; + int resultB = 20; + + int countB = MakingAnagrams.makeAnagrams(b1, b2); + assertEquals(countB, resultB); + } + + @Test + void testWithStringAllCharacter() { + String a1 = "1234567890 ~!@#$%^"; + String a2 = "&*()-+1234567890 "; + int resultA = 13; + + int countA = MakingAnagrams.makeAnagrams(a1, a2); + assertEquals(countA, resultA); + + String b1 = "asdfghjklb1234567890 "; + String b2 = "1234567890 qwertyuiop"; + int resultB = 20; + + int countB = MakingAnagrams.makeAnagrams(b1, b2); + assertEquals(countB, resultB); + + String c1 = "hello my world"; + String c2 = "this is my world"; + int resultC = 10; + + int countC = MakingAnagrams.makeAnagrams(c1, c2); + assertEquals(countC, resultC); + } + +}