Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
679 B
1
Indexable
Never
class Solution {
public:
    int wordCount(vector<string>& startWords, vector<string>& targetWords) {
        unordered_set<string>us;
        for(auto sw: startWords){
            sort(sw.begin(), sw.end());
            us.insert(sw);
        }
        int ans = 0;
        for(auto tw: targetWords){
            sort(tw.begin(), tw.end());
            for(int i = 0; i < tw.size(); i++){
                string tmp = tw.substr(0, i) + tw.substr(i+1);
                //1 char diff, use substr
                if(us.count(tmp)){
                    ans += 1;
                    break;
                }
            }
        }
        return ans;
    }
};
Leave a Comment