Skip to content

Commit 91aafb2

Browse files
Update test case TestCheckPermutation, TestMakingAnagram and update class CheckPermutation
1 parent 957d03b commit 91aafb2

File tree

3 files changed

+160
-2
lines changed

3 files changed

+160
-2
lines changed

src/main/java/com/ctci/arraysandstrings/CheckPermutation.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class CheckPermutation {
1515
* @param s2
1616
* @return
1717
*/
18-
private static boolean isOnePermutationOfOther(String s1, String s2) {
18+
public static boolean isOnePermutationOfOther(String s1, String s2) {
1919
if (s1.length() != s2.length()) {
2020
return false;
2121
}
@@ -35,18 +35,24 @@ private static boolean isOnePermutationOfOther(String s1, String s2) {
3535
* @param s2
3636
* @return
3737
*/
38-
private static boolean isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(String s1, String s2) {
38+
public static boolean isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(String s1, String s2) {
3939
if (s1.length() != s2.length()) {
4040
return false;
4141
}
4242

4343
int[] chars = new int[128]; // assuming strings contain only ASCII characters
4444

4545
for (int i = 0; i < s1.length(); i++) {
46+
if(s1.charAt(i) < 0 || s1.charAt(i) >= 128) {
47+
return false;
48+
}
4649
chars[s1.charAt(i)]++;
4750
}
4851

4952
for (int i = 0; i < s2.length(); i++) {
53+
if(s2.charAt(i) < 0 || s2.charAt(i) >= 128) {
54+
return false;
55+
}
5056
chars[s2.charAt(i)]--;
5157
if (chars[s2.charAt(i)] < 0) {
5258
return false;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.ctci.arraysandstrings;
2+
3+
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class TestCheckPermutation {
10+
11+
@Test
12+
void testPermutationStringWithEmpty() {
13+
String a1 = "";
14+
String a2 = "";
15+
16+
String b1 = "1234567890 qwertyuiopasdfghjklzxcvbnm !@#$%^&*()-=";
17+
String b2 = "";
18+
19+
String c1 = "";
20+
String c2 = "1234567890 qwertyuiopasdfghjklzxcvbnm !@#$%^&*()-=";
21+
22+
23+
boolean resultA = CheckPermutation.isOnePermutationOfOther(a1, a2);
24+
boolean resultB = CheckPermutation.isOnePermutationOfOther(b1, b2);
25+
boolean resultC = CheckPermutation.isOnePermutationOfOther(c1, c2);
26+
27+
assertTrue(resultA);
28+
assertFalse(resultB);
29+
assertFalse(resultC);
30+
}
31+
32+
@Test
33+
void testPermutationStringAllCharacter() {
34+
String a1 = "1234567890 qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM !@#$%^&*()-=";
35+
String a2 = "!@#$%^&*()-= 1234567890 qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM";
36+
37+
String b1 = "~!@#$%^&*([{<>}])+-=:;'\\\",./? ";
38+
String b2 = " ?/.,\"\\';:=-+)}]><[{(*&^%$#@!~";
39+
40+
String c1 = "Â Ă Ơ Ô Ư Ê â ă ơ ô ư ê";
41+
String c2 = "ê ư ô ơ ă â Ê Ư Ô Ơ Ă Â";
42+
43+
String d1 = "ÂÂ ĂĂ ƠƠ ÔÔ ƯƯ ÊÊ ââ ăă ơơ ôô ưư êê";
44+
String d2 = "ê ư ô ơ ă â Ê Ư Ô Ơ Ă Â";
45+
46+
47+
boolean resultA = CheckPermutation.isOnePermutationOfOther(a1, a2);
48+
boolean resultB = CheckPermutation.isOnePermutationOfOther(b1, b2);
49+
boolean resultC = CheckPermutation.isOnePermutationOfOther(c1, c2);
50+
boolean resultD = CheckPermutation.isOnePermutationOfOther(d1, d2);
51+
52+
assertTrue(resultA);
53+
assertTrue(resultB);
54+
assertTrue(resultC);
55+
assertFalse(resultD);
56+
}
57+
58+
@Test
59+
void testPermutationWithAsciiAndStringEmpty() {
60+
String a1 = "";
61+
String a2 = "";
62+
63+
String b1 = "1234567890 qwertyuiopasdfghjklzxcvbnm !@#$%^&*()-=";
64+
String b2 = "";
65+
66+
String c1 = "";
67+
String c2 = "1234567890 qwertyuiopasdfghjklzxcvbnm !@#$%^&*()-=";
68+
69+
70+
boolean resultA = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(a1, a2);
71+
boolean resultB = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(b1, b2);
72+
boolean resultC = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(c1, c2);
73+
74+
assertTrue(resultA);
75+
assertFalse(resultB);
76+
assertFalse(resultC);
77+
}
78+
79+
@Test
80+
void testPermutationWithAsciiAndStringAllCharacter() {
81+
String a1 = "1234567890 qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM !@#$%^&*()-=";
82+
String a2 = "!@#$%^&*()-= 1234567890 qwertyuiopasdfghjklzxcvbnm QWERTYUIOPASDFGHJKLZXCVBNM";
83+
84+
String b1 = "~!@#$%^&*([{<>}])+-=:;'\\\",./? ";
85+
String b2 = " ?/.,\"\\';:=-+)}]><[{(*&^%$#@!~";
86+
87+
String c1 = "Â Ă Ơ Ô Ư Ê â ă ơ ô ư ê";
88+
String c2 = "ê ư ô ơ ă â Ê Ư Ô Ơ Ă Â";
89+
90+
String d1 = "Â Ă Ơ Ô Ư Ê â ă ơ ô ư ê";
91+
String e2 = "ê ư ô ơ ă â Ê Ư Ô Ơ Ă Â";
92+
93+
94+
boolean resultA = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(a1, a2);
95+
boolean resultB = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(b1, b2);
96+
boolean resultC = CheckPermutation.isOnePermutationOfOtherGivenThatStringsContainOnlyAscii(c1, c2);
97+
98+
assertTrue(resultA);
99+
assertTrue(resultB);
100+
assertFalse(resultC);
101+
}
102+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.hackerrank.algorithms.strings;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class TestMakingAnagrams {
8+
9+
@Test
10+
void testWithStringEmpty() {
11+
String a1 = "";
12+
String a2 = "";
13+
int resultA = 0;
14+
15+
int countA = MakingAnagrams.makeAnagrams(a1, a2);
16+
assertEquals(countA, resultA);
17+
18+
String b1 = "";
19+
String b2 = "1234567890qwertyuiop";
20+
int resultB = 20;
21+
22+
int countB = MakingAnagrams.makeAnagrams(b1, b2);
23+
assertEquals(countB, resultB);
24+
}
25+
26+
@Test
27+
void testWithStringAllCharacter() {
28+
String a1 = "1234567890 ~!@#$%^";
29+
String a2 = "&*()-+1234567890 ";
30+
int resultA = 13;
31+
32+
int countA = MakingAnagrams.makeAnagrams(a1, a2);
33+
assertEquals(countA, resultA);
34+
35+
String b1 = "asdfghjklb1234567890 ";
36+
String b2 = "1234567890 qwertyuiop";
37+
int resultB = 20;
38+
39+
int countB = MakingAnagrams.makeAnagrams(b1, b2);
40+
assertEquals(countB, resultB);
41+
42+
String c1 = "hello my world";
43+
String c2 = "this is my world";
44+
int resultC = 10;
45+
46+
int countC = MakingAnagrams.makeAnagrams(c1, c2);
47+
assertEquals(countC, resultC);
48+
}
49+
50+
}

0 commit comments

Comments
 (0)