Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int freq[30];
- bool visited[30];
- int main () {
- string s;
- cin >> s;
- for (int i = 0; i < s.size(); ++i) {
- freq[ s[i] - 'a' ]++;
- // here we make it freq[ s[i] - 'a' ]
- // if s[i] = 'a'
- // freq[ s[i] - 'a' ]++ = freq[ 'a' - 'a' ]++ = freq[0]++
- // if s[i] = 'b'
- // freq[ s[i] - 'a' ]++ = freq[ 'b' - 'a' ]++ = freq[1]++
- // and so on
- // so we will reduce each by char 'a'
- // 'a' = 97
- // if the characters of the string was CAPITAL we will replace 'a' with 'A'
- }
- for (int i = 0; i < s.size(); ++i) {
- if (!visited[s[i] - 'a']) {
- // visited array
- // to avoid print same char and his frequency more than once
- visited[s[i] - 'a'] = true;
- cout << s[i] << " " << freq[s[i] - 'a'] << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement