I need to run an efficient function to generate string combinations. I used multiple answers in SO to write something that works. The vectors are combined, the resultant strings then sorted by character and then duplicates are removed by inserting the resultant vector into an unordered set. The function needs to run 1e7 times on long vectors (100K) and I need help running more efficient code. Here's what I have now:
vector<string> vec_combos(vector<string> v1, vector<string> v2) {
vector<string> res;
unordered_set<string> s;
for(int i=0;i<v1.size();i++) {
for(int j=0;j<v2.size();j++){
res.push_back(v1[i]+v2[j]);
sort(res.back().begin(),res.back().end());
}
}
for( int i = 0; i < res.size(); ++i ) s.insert( res[i] );
res.assign( s.begin(), s.end() );
return res;
}
int main() {
vector<string> v1 = {"ab","bc","ca"}, v2 = {"a","b"},v3;
// combined vector = {"aba","bca","caa","abb","bcb","cab"}
// sort string chars = {"aab","abc","aac","abb","bbc","abc"}
// remove duplicates = {"aab","abc","aac","abb","bbc"}
v3 = vec_combos(v1, v2);
for(int i=0;i<v3.size();i++) {
cout << v3[i] << ",";
}
return(0);
}