737. Sentence Similarity II

Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar.

For example, words1 = [“great”, “acting”, “skills”] and words2 = [“fine”, “drama”, “talent”] are similar, if the similar word pairs are pairs = [[“great”, “good”], [“fine”, “good”], [“acting”,”drama”], [“skills”,”talent”]].

Note that the similarity relation is transitive. For example, if “great” and “good” are similar, and “fine” and “good” are similar, then “great” and “fine” are similar.

Similarity is also symmetric. For example, “great” and “fine” being similar is the same as “fine” and “great” being similar.

Also, a word is always similar with itself. For example, the sentences words1 = [“great”], words2 = [“great”], pairs = [] are similar, even though there are no specified similar word pairs.

Finally, sentences can only be similar if they have the same number of words. So a sentence like words1 = [“great”] can never be similar to words2 = [“doubleplus”,”good”].

其实这题本质就是建立一个无向图,跟找连通分量是一回事。将pairs里出现的每一个单词当作一个点,每一个组合当作一条线,只要两个单词之间可以有路径形成,就是一个近义词,也就是两个单词处于一个连通分量里。所以深度优先搜索就行了。

方法一:递归写法

class Solution {
public:
    bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
        int len = words1.size();
        if (words2.size() != len) return false;

        unordered_map<string, vector<string>> hash;
        unordered_set<string> isVisited;
        for (auto p : pairs) {
            hash[p.first].push_back(p.second);
            hash[p.second].push_back(p.first);
        }

        for (int i = 0; i < len; i++) {
            if (words1[i] == words2[i]) continue;
            isVisited.clear();
            if (DFS(words1[i], words2[i], hash, isVisited)) continue;
            return false;
        }
        return true;
    }

    bool DFS(string s1, string s2, unordered_map<string, vector<string>>& hash, unordered_set<string>& isVisited) {
        if (isVisited.find(s1) == isVisited.end()) {
            isVisited.emplace(s1);
            for (auto s : hash[s1]) {
                if (s2 == s) return true;
                if (DFS(s, s2, hash, isVisited)) return true;
            }
            return false;
        }
        return false;
    }
};

方法二:非递归写法

class Solution {
public:
    bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
        int len = words1.size();
        if (words2.size() != len) return false;

        unordered_map<string, vector<string>> hash;
        unordered_set<string> isVisited;
        for (auto p : pairs) {
            hash[p.first].push_back(p.second);
            hash[p.second].push_back(p.first);
        }

        for (int i = 0; i < len; i++) {
            if (words1[i] == words2[i]) continue;
            isVisited.clear();
            stack<string> st;
            st.push(words1[i]);
            isVisited.emplace(words1[i]);
            bool flag = false;
            while (!st.empty() && flag == false) {
                string w1 = st.top();
                st.pop();
                for (auto s : hash[w1]) {
                    if (isVisited.find(s) == isVisited.end()) {
                        if (s == words2[i]) {
                            flag = true; 
                            break;
                        }
                        isVisited.emplace(s);
                        st.push(s);
                    }
                }
            }
            if (flag == false) return false;
        }
        return true;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值