Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
thangtran180492 wants to merge1 commit intorampatra:master
base:master
Choose a base branch
Loading
fromthangtran180492:master
Open
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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;
}
Expand All@@ -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;
Expand Down
102 changes: 102 additions & 0 deletionssrc/main/java/com/ctci/arraysandstrings/TestCheckPermutation.java
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff 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);
}

}

[8]ページ先頭

©2009-2025 Movatter.jp