parent
8be07c6b36
commit
c93173406e
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.uniquecharcheck;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class UniqueCharChecker {
|
||||
|
||||
public static boolean checkV1(String str) {
|
||||
char[] chars = str.toUpperCase().toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
for (int j = i + 1; j < chars.length; j++) {
|
||||
if(chars[i] == chars[j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkV2(String str) {
|
||||
char[] chars = str.toUpperCase().toCharArray();
|
||||
Arrays.sort(chars);
|
||||
for (int i = 0; i < chars.length - 1; i++) {
|
||||
if(chars[i] == chars[i+1]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkV3(String str) {
|
||||
char[] chars = str.toUpperCase().toCharArray();
|
||||
Set <Character> set = new HashSet <>();
|
||||
for (char c: chars) {
|
||||
set.add(c);
|
||||
}
|
||||
return set.size() == str.length();
|
||||
}
|
||||
|
||||
public static boolean checkV4(String str) {
|
||||
boolean isUnique = str.toUpperCase().chars()
|
||||
.mapToObj(c -> (char)c)
|
||||
.collect(Collectors.toSet())
|
||||
.size() == str.length();
|
||||
return isUnique;
|
||||
}
|
||||
|
||||
public static boolean checkV5(String str) {
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
String curChar = String.valueOf(str.charAt(i));
|
||||
String remainingStr = str.substring(i+1);
|
||||
if(StringUtils.containsIgnoreCase(remainingStr, curChar)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package com.baeldung.uniquecharcheck;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class UniqueCharCheckerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMethCheck1_whenUnique_returnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV1(sampleStr)));
|
||||
}
|
||||
@Test
|
||||
public void givenMethCheck2_whenUnique_returnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV2(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck3_whenUnique_returnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV3(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck4_whenUnique_returnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV1(sampleStr)));
|
||||
}
|
||||
@Test
|
||||
public void givenMethCheck5_whenUnique_returnTrue() {
|
||||
String[] sampleStrings = new String[]{"Justfewdi123", "$%&Hibusc", "Hibusc%$#", "მშვნიერ"};
|
||||
final String MSG = "Duplicate found";
|
||||
Arrays.stream(sampleStrings)
|
||||
.forEach(sampleStr -> assertTrue(MSG + " in " + sampleStr, UniqueCharChecker.checkV5(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck1_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.checkV1(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck2_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.checkV2(sampleStr)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck3_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.checkV3(sampleStr)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethCheck4_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.checkV4(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)));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue