Skip to content
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
4 changes: 2 additions & 2 deletions src/main/java/com/ctci/arraysandstrings/IsUnique.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
public class IsUnique {

private static boolean hasAllUniqueCharacters(String str) {
public static boolean hasAllUniqueCharacters(String str) {
if (str == null || str.length() > 128) return false;

boolean[] charSet = new boolean[128]; // assuming the string contains only ASCII characters
Expand All @@ -20,7 +20,7 @@ private static boolean hasAllUniqueCharacters(String str) {
return true;
}

private static boolean hasAllUniqueCharactersWhenStringContainsAllLowercase(String s) {
public static boolean hasAllUniqueCharactersWhenStringContainsAllLowercase(String s) {
int checker = 0;
for (int i = 0; i < s.length(); i++) {
int charValue = s.charAt(i) - 'a';
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/ctci/arraysandstrings/TestIsUnique.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ctci.arraysandstrings;

import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class TestIsUnique {
@Test
public void TesthasAllUniqueCharacters() {
String TC1 = "qwertyuiop123456@#$%";
String TC2 = "aaaaaaaaa";

boolean R1 = IsUnique.hasAllUniqueCharacters(TC1);
boolean R2 = IsUnique.hasAllUniqueCharacters(TC2);

assertTrue(R1);
assertFalse(R2);
}
@Test
public void TesthasAllUniqueCharactersWhenStringContainsAllLowercase() {
String TC1 = "qwertyuiop";
String TC2 = "aaaaaaaaa";
String TC3 = "QwertHfakjf";

boolean R1 = IsUnique.hasAllUniqueCharactersWhenStringContainsAllLowercase(TC1);
boolean R2 = IsUnique.hasAllUniqueCharactersWhenStringContainsAllLowercase(TC2);
boolean R3 = IsUnique.hasAllUniqueCharactersWhenStringContainsAllLowercase(TC3);

assertTrue(R1);
assertFalse(R2);
assertFalse(R3);

}
}
15 changes: 15 additions & 0 deletions src/main/java/com/ctci/arraysandstrings/testMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ctci.arraysandstrings;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class testMain {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestIsUnique.class);
for (Failure failure: result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}