-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHashing.java
More file actions
79 lines (47 loc) · 1.61 KB
/
Copy pathStringHashing.java
File metadata and controls
79 lines (47 loc) · 1.61 KB
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
71
72
73
74
75
76
77
78
79
/*
Hashing
Time complexity
================
Ordered Map : Best case, worst case and average case TC is O(log n)
Unordered Map : Best case, average case TC is O(1) and worst case is O(n)
Implementation
===============
*/
import java.io.*;
import java.util.*;
public class StringHashing{
//Logic if all character in given string are lower case
public static int checkCharacterOccurranceInGivenString(String s, char key){
//hash array
int[] hash = new int[26];
Arrays.fill(hash,0);
//creation of hash array
for(char ch : s.toCharArray()){
hash[ch - 'a']++;
}
return hash[key-'a'];
}
//Logic if all character in given string are either lower case or upper case
public static int checkCharacterOccurranceInGivenStringSplCase(String s, char key){
//hash array
int[] hash = new int[256];
Arrays.fill(hash,0);
//creation of hash array
for(char ch : s.toCharArray()){
hash[ch]++;
}
return hash[key];
}
public static void main(String[] args){
String s = "samuell";
char key = 'l';
System.out.println("The occurrence of letter "+ key +" is :" +checkCharacterOccurranceInGivenString(s,key));
s = " SaHRRUreLL";
key = 'r';
System.out.println("The occurrence of letter "+ key +" is :" +checkCharacterOccurranceInGivenStringSplCase(s,key));
}
}
/* o/p:-
The occurrence of letter l is :2
The occurrence of letter r is :1
*/