From dea372fd5a872eb836e8ac4c7dcf06344def8bbb Mon Sep 17 00:00:00 2001 From: Nirmal Silwal Date: Mon, 26 Sep 2022 19:28:20 -0500 Subject: [PATCH] Create UniqueDuplicates.java kitestring company --- Interview Prep/UniqueDuplicates.java | 37 ++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Interview Prep/UniqueDuplicates.java diff --git a/Interview Prep/UniqueDuplicates.java b/Interview Prep/UniqueDuplicates.java new file mode 100644 index 0000000..7f28111 --- /dev/null +++ b/Interview Prep/UniqueDuplicates.java @@ -0,0 +1,37 @@ +package kitestring; + +import java.util.HashSet; +import java.util.Set; + +public class UniqueDuplicates { + + public static void main(String[] args) { + String str = "aabbcccdd"; + System.out.println(uniqueDups(str)); + System.out.println(uniqueDups("aabbbcccccd")); + System.out.println(uniqueDups("abbcccddddeeeee")); + + } + + private static boolean uniqueDups(String str) { + int[] chars = new int[26]; + + for (char ch : str.toCharArray()) { + int pos = ch - 'a'; + chars[pos]++; + } + Set set = new HashSet<>(); + + + for (int n : chars) { + if (n > 0) { // if character exist + if (!set.contains(n)) + set.add(n); + else + return false; + } + } + return true; + } + +}