-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frequency_of_String.cpp
111 lines (100 loc) · 2.16 KB
/
Frequency_of_String.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <bits/stdc++.h>
#include <map>
using namespace std;
class freq
{
public:
static void LowerCaseLetterFrequency(string &s)
{
int q;
int hash[27] = {0};
cin >> q;
for (int i = 0; i < s.size(); i++)
{
hash[s[i] - 'a']++;
}
while (q--)
{
char c;
cin >> c;
cout << hash[c - 'a'] << endl;
}
}
static void UpperCaseLetterFrequency(string &s)
{
int q;
int hash[27] = {0};
cin >> q;
for (int i = 0; i < s.size(); i++)
{
hash[s[i] - 'A']++;
}
while (q--)
{
char c;
cin >> c;
cout << hash[c - 'A'] << endl;
}
}
static void MixedCaseLetterFrequency(string &s)
{
int q;
int hash[256] = {0};
cin >> q;
for (int i = 0; i < s.size(); i++)
{
hash[s[i]]++;
}
while (q--)
{
char c;
cin >> c;
cout << hash[c] << endl;
}
}
static void Array_occur(int arr[], int n)
{
unordered_map<int, int> mpp;
for (int i = 0; i < n; i++)
{
mpp[arr[i]]++;
}
// Iterate the map
for (auto it : mpp)
{
cout << it.first << " -> " << it.second << endl;
}
int q;
cin >> q;
while (q--)
{
int num;
cin >> num;
cout << mpp[num] << endl;
}
}
};
int main()
{
freq obj = freq();
/* string str;
int query;
cout << "Enter String : " << endl;
cin >> str;
obj.LowerCaseLetterFrequency(str);
obj.UpperCaseLetterFrequency(str);
obj.MixedCaseLetterFrequency(str); */
int n;
cout << "Enter size of the array : ";
cin >> n;
int arr[n];
cout << endl
<< "Enter Array : " << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
obj.Array_occur(arr, n);
return 0;
}