Skip to content

Commit

Permalink
Create UniqueDuplicates.java kitestring company
Browse files Browse the repository at this point in the history
  • Loading branch information
NirmalSilwal committed Sep 27, 2022
1 parent b043329 commit dea372f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Interview Prep/UniqueDuplicates.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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;
}

}

0 comments on commit dea372f

Please sign in to comment.