parent
f78b9f0b9b
commit
4f24eb635e
|
@ -7,7 +7,7 @@ import java.util.stream.Collectors;
|
|||
|
||||
public class UniqueCharChecker {
|
||||
|
||||
public static boolean checkV1(String str) {
|
||||
public static boolean bruteForceCheck(String str) {
|
||||
char[] chars = str.toUpperCase().toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
for (int j = i + 1; j < chars.length; j++) {
|
||||
|
@ -19,7 +19,7 @@ public class UniqueCharChecker {
|
|||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkV2(String str) {
|
||||
public static boolean sortAndThenCheck(String str) {
|
||||
char[] chars = str.toUpperCase().toCharArray();
|
||||
Arrays.sort(chars);
|
||||
for (int i = 0; i < chars.length - 1; i++) {
|
||||
|
@ -30,7 +30,7 @@ public class UniqueCharChecker {
|
|||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkV3(String str) {
|
||||
public static boolean useSetCheck(String str) {
|
||||
char[] chars = str.toUpperCase().toCharArray();
|
||||
Set <Character> set = new HashSet <>();
|
||||
for (char c: chars) {
|
||||
|
@ -39,7 +39,7 @@ public class UniqueCharChecker {
|
|||
return set.size() == str.length();
|
||||
}
|
||||
|
||||
public static boolean checkV4(String str) {
|
||||
public static boolean useStreamCheck(String str) {
|
||||
boolean isUnique = str.toUpperCase().chars()
|
||||
.mapToObj(c -> (char)c)
|
||||
.collect(Collectors.toSet())
|
||||
|
@ -47,7 +47,7 @@ public class UniqueCharChecker {
|
|||
return isUnique;
|
||||
}
|
||||
|
||||
public static boolean checkV5(String str) {
|
||||
public static boolean useStringUtilscheck(String str) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
String curChar = String.valueOf(str.charAt(i));
|
||||
String remainingStr = str.substring(i+1);
|
||||
|
|
|
@ -11,82 +11,82 @@ import static org.junit.Assert.assertTrue;
|
|||
public class UniqueCharCheckerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMethCheck1_whenUnique_returnTrue() {
|
||||
public void givenUnique_whenBruteForceCheck_thenReturnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV1(sampleStr)));
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.bruteForceCheck(sampleStr)));
|
||||
}
|
||||
@Test
|
||||
public void givenMethCheck2_whenUnique_returnTrue() {
|
||||
public void givenUnique_whenSortAndThenCheck_thenReturnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV2(sampleStr)));
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.sortAndThenCheck(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck3_whenUnique_returnTrue() {
|
||||
public void givenUnique_whenUseSetCheck_thenReturnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV3(sampleStr)));
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.useSetCheck(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck4_whenUnique_returnTrue() {
|
||||
public void givenUnique_whenUseStreamCheck_thenReturnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV1(sampleStr)));
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.useStreamCheck(sampleStr)));
|
||||
}
|
||||
@Test
|
||||
public void givenMethCheck5_whenUnique_returnTrue() {
|
||||
public void givenUnique_whenUseStringUtilscheck_thenReturnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV5(sampleStr)));
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.useStringUtilscheck(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck1_whenNotUnique_returnFalse() {
|
||||
public void givenNotUnique_whenBruteForceCheck_thenReturnFalse() {
|
||||
String[] sampleStrings = new String[]{"Justfewdif123", "$%&Hibushc", "Hibusuc%$#", "Hi%busc%$#", "მშვენიერი"};
|
||||
final String MSG = "Duplicate not found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.checkV1(sampleStr)));
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.bruteForceCheck(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck2_whenNotUnique_returnFalse() {
|
||||
public void givenNotUnique_whenSortAndThenCheck_thenReturnFalse() {
|
||||
String[] sampleStrings = new String[]{"Justfewdif123", "$%&Hibushc", "Hibusuc%$#", "Hi%busc%$#", "მშვენიერი"};
|
||||
final String MSG = "Duplicate not found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.checkV2(sampleStr)));
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.sortAndThenCheck(sampleStr)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck3_whenNotUnique_returnFalse() {
|
||||
public void givenNotUnique_whenUseSetCheck_thenReturnFalse() {
|
||||
String[] sampleStrings = new String[]{"Justfewdif123", "$%&Hibushc", "Hibusuc%$#", "Hi%busc%$#", "მშვენიერი"};
|
||||
final String MSG = "Duplicate not found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.checkV3(sampleStr)));
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.useSetCheck(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck4_whenNotUnique_returnFalse() {
|
||||
public void givenNotUnique_whenUseStreamCheck_thenReturnFalse() {
|
||||
String[] sampleStrings = new String[]{"Justfewdif123", "$%&Hibushc", "Hibusuc%$#", "Hi%busc%$#", "მშვენიერი"};
|
||||
final String MSG = "Duplicate not found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.checkV4(sampleStr)));
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.useStreamCheck(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck5_whenNotUnique_returnFalse() {
|
||||
String[] sampleStrings = new String[]{"Justfewdif123", "$%&Hibushc", "Hibusuc%$#", "Hi%busc%$#", "მშვენიერი"};
|
||||
final String MSG = "Duplicate not found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.checkV5(sampleStr)));
|
||||
}
|
||||
@Test
|
||||
public void givenNotUnique_whenUseStringUtilscheck_thenReturnFalse() {
|
||||
String[] sampleStrings = new String[]{"Justfewdif123", "$%&Hibushc", "Hibusuc%$#", "Hi%busc%$#", "მშვენიერი"};
|
||||
final String MSG = "Duplicate not found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertFalse(MSG + " in " + sampleStr, UniqueCharChecker.useStringUtilscheck(sampleStr)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue