-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuplicateCharsInString.java
53 lines (44 loc) · 1.56 KB
/
DuplicateCharsInString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package tomkous.algos;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DuplicateCharsInString {
public void findDuplicateChars(String str){
Map<Character, Integer> dupMap = new HashMap<Character, Integer>();
char[] chrs = str.toCharArray();
for(Character ch:chrs){
if(dupMap.containsKey(ch)){
dupMap.put(ch, dupMap.get(ch)+1);
} else {
dupMap.put(ch, 1);
}
}
Set<Character> keys = dupMap.keySet();
for(Character ch:keys){
if(dupMap.get(ch) > 1){
System.out.println(ch+"--->"+dupMap.get(ch));
}
}
}
public boolean findIfDups(String str){
Map<Character, Integer> dupMap = new HashMap<Character, Integer>();
char[] chrs = str.toCharArray();
for(Character ch:chrs){
if(dupMap.containsKey(ch)){
dupMap.put(ch, dupMap.get(ch)+1);
return true;
} else {
dupMap.put(ch, 1);
}
}
return false;
}
public static void main(String a[]){
DuplicateCharsInString dcs = new DuplicateCharsInString();
dcs.findDuplicateChars("Our time is running out");
if(dcs.findIfDups("I think I'm drowned, asphyxiated")){
System.out.println("I want to break a spell that you've created");
System.out.println("Love! Is our reeeeesistance!");
}
}
}