Untitled

 avatar
unknown
c_cpp
a year ago
679 B
5
Indexable
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;
    }
};
Editor is loading...
Leave a Comment