从 一 string 中找出所有能组成target string的个数。比如catcdeaht, 找能组成cat的个数
// reverse algorithm example #include <iostream> // std::cout #include <algorithm> // std::reverse #include <vector> // std::vector #include <unordered_map> //从 一 string 中找出所有能组成target string的个数。比如catcdeaht, 找能组成cat的个数 //key point // unordered_map<char , int > statistic the number of the each char, which is existed in string t, in string & s. // the min number of the char is answer. using namespace std; int func(string&s , string&t){ unordered_map<char, int > hashmap; for(auto&e : t) hashmap[e] = 0; for (auto&e:s){ if(hashmap.find(e) != hashmap.end()) hashmap[e] += 1; } int min = INT_MAX; for (auto& e: hashmap) min = min < e.second? min: e.second; return min; } int main( int argc, char** arg){ string in = {"cathellocheat"}; string t = {"cat"}; cout<<"the number is " << func(in,t) << endl; }