/*
Extract all the the combinations of a target substring that appear in a source string. I.e: target:
"abc", source:"abcdefgbcahijkacb12df", solution: {abc, bca, acb}
*/
// compute a hash value for a string. this function may be only suitable for letters from 'a' to 'z'
long long hash_fun(const string &t) {
if(t.empty()) return 0;
long long hash_val = 0;
string s = t;
sort(s.begin(), s.end());
for(char c : s) {
hash_val = 26*hash_val + c - 'a';
}
return hash_val;
}
// another hash function. can handle any char.
long long hash_fun2(const string &t) {
if(t.empty()) return 0;
long long hash_val = 0;
multiset<char> mset; // note, multiset belongs to #include <set>
for(char c : t) mset.insert(c);
for(char c : mset)
hash_val = 26 * hash_val + c - 'a';
return hash_val;
}
vector<string> extract_combinations(const string& s, const string& p) {
vector<string> res;
if(s.empty() || p.empty() || s.size() < p.size()) return res;
long long hash_val = 0;
int Ns = s.size(), Np = p.size();
long long hash_val_p = hash_fun(p);
for(int i=0; i<Ns - Np; i++) {
string sub = s.substr(i, Np);
if(hash_fun(sub) == hash_val_p)
res.push_back(sub);
}
return res;
}
int main()
{
string p = "abc";
vector<string> vs = extract_combinations("abcdefgbcahijkacb12df", p);
for(auto s : vs) cout << s << endl;
return 0;
}