View Code of Problem 22

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int arr[26];

void init() {
    for (int i = 0; i < 26; ++i){
        arr[i] = 0;
    }
}

int main() {
    string str1;
    string str2;
    while (getline(cin,str1) && getline(cin,str2)) {

        init();
        vector<char> result;
        for (int i = 0; i < str2.length(); ++i) {
            if (str2[i] >= 'a' && str2[i] <= 'z'){
                 arr[str2[i] - 'a'] ++;
            }
        }
        for (int i = 0; i < str1.length(); ++i) {
            if (str1[i] >= 'a' && str1[i] <= 'z'){
                if (arr[str1[i] - 'a'] != 0) {
                    result.push_back(str1[i]);
                    arr[str1[i] - 'a'] --;
                }
            }
        }
        sort(result.begin(),result.end());
        for (int i = 0; i < result.size(); ++i) {
            cout << result[i];
        }
        cout << endl;
    }
}

Double click to view unformatted code.


Back to problem 22