Skip to content

Update test case #42

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/main/java/com/ctci/arraysandstrings/CheckPermutation.java
Original file line number Diff line number Diff line change
@@ -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,18 +35,24 @@ 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;
}

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;
102 changes: 102 additions & 0 deletions src/main/java/com/ctci/arraysandstrings/TestCheckPermutation.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}