Advertisement
Hamoudi30

Untitled

May 29th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int freq[30];
  4. bool visited[30];
  5. int main () {
  6.     string s;
  7.     cin >> s;
  8.     for (int i = 0; i < s.size(); ++i) {
  9.         freq[ s[i] - 'a' ]++;
  10.         // here we make it freq[ s[i] - 'a' ]
  11.         // if s[i] = 'a'
  12.         // freq[ s[i] - 'a' ]++ = freq[ 'a' - 'a' ]++ = freq[0]++
  13.         // if s[i] = 'b'
  14.         // freq[ s[i] - 'a' ]++ = freq[ 'b' - 'a' ]++ = freq[1]++
  15.         // and so on
  16.         // so we will reduce each by char 'a'
  17.         // 'a' = 97
  18.         // if the characters of the string was CAPITAL we will replace 'a' with 'A'
  19.     }
  20.    for (int i = 0; i < s.size(); ++i) {
  21.         if (!visited[s[i] - 'a']) {
  22.             // visited array
  23.             // to avoid print same char and his frequency more than once  
  24.             visited[s[i] - 'a'] = true;
  25.             cout << s[i] << " " << freq[s[i] - 'a'] << endl;
  26.         }
  27.     }
  28.     return 0;
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement