File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ package kitestring ;
2+
3+ import java .util .HashSet ;
4+ import java .util .Set ;
5+
6+ public class UniqueDuplicates {
7+
8+ public static void main (String [] args ) {
9+ String str = "aabbcccdd" ;
10+ System .out .println (uniqueDups (str ));
11+ System .out .println (uniqueDups ("aabbbcccccd" ));
12+ System .out .println (uniqueDups ("abbcccddddeeeee" ));
13+
14+ }
15+
16+ private static boolean uniqueDups (String str ) {
17+ int [] chars = new int [26 ];
18+
19+ for (char ch : str .toCharArray ()) {
20+ int pos = ch - 'a' ;
21+ chars [pos ]++;
22+ }
23+ Set <Integer > set = new HashSet <>();
24+
25+
26+ for (int n : chars ) {
27+ if (n > 0 ) { // if character exist
28+ if (!set .contains (n ))
29+ set .add (n );
30+ else
31+ return false ;
32+ }
33+ }
34+ return true ;
35+ }
36+
37+ }
You can’t perform that action at this time.
0 commit comments