-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountCharInAString.java
70 lines (50 loc) · 1.58 KB
/
CountCharInAString.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package interviewquestions;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;
public class CountCharInAString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter string: ");
String s = sc.nextLine();
s = s.toLowerCase();
//<key, value> -> <character, its count> -> <Character, Integer>
LinkedHashMap<Character, Integer> map = new LinkedHashMap<Character, Integer>();
//go thro each char in a string
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i); //getting a char
//System.out.println("getting a char: " + c);
if (map.containsKey(c)) {
int prevCount = map.get(c);
map.put(c, ++prevCount);
//System.out.println("Character is there so updating asmap.put("+ c + ", " + prevCount + ")");
} else {
map.put(c, 1);
//System.out.println("Character is not there so updating as map.put("+ c + ", " + 1+ ")");
}
//System.out.println("now hash map: " + map);
}
for (Character c : map.keySet()) {
// value = map.get(key)
System.out.println(c + " -> " + map.get(c));
}
// first repeated char
// occurence/ count > 1
//java is awesome -> a, s, e
// its comes first -> a
for (Character c : map.keySet()) {
if (map.get(c) > 1) {
System.out.println("First Repeated Character is " + c);
break;
}
}
// first non repeated char
// occurence == 1
for (Character c : map.keySet()) {
if (map.get(c) == 1) {
System.out.println("First Non-repeated Character is " + c);
break;
}
}
}
}