From c93173406eaa965742c4eacf22d13633389b564b Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Fri, 14 Jul 2023 01:19:18 +0530 Subject: [PATCH] BAEL-6710 (#14403) Check If a String Has All Unique Characters in Java --- .../uniquecharcheck/UniqueCharChecker.java | 60 ++++++++++++ .../UniqueCharCheckerUnitTest.java | 92 +++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/uniquecharcheck/UniqueCharChecker.java create mode 100644 core-java-modules/core-java-string-operations-6/src/test/java/com/baeldung/uniquecharcheck/UniqueCharCheckerUnitTest.java diff --git a/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/uniquecharcheck/UniqueCharChecker.java b/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/uniquecharcheck/UniqueCharChecker.java new file mode 100644 index 0000000000..365307708c --- /dev/null +++ b/core-java-modules/core-java-string-operations-6/src/main/java/com/baeldung/uniquecharcheck/UniqueCharChecker.java @@ -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 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; + } +} diff --git a/core-java-modules/core-java-string-operations-6/src/test/java/com/baeldung/uniquecharcheck/UniqueCharCheckerUnitTest.java b/core-java-modules/core-java-string-operations-6/src/test/java/com/baeldung/uniquecharcheck/UniqueCharCheckerUnitTest.java new file mode 100644 index 0000000000..938787662f --- /dev/null +++ b/core-java-modules/core-java-string-operations-6/src/test/java/com/baeldung/uniquecharcheck/UniqueCharCheckerUnitTest.java @@ -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))); +} + +}