737. Sentence Similarity II

来源:互联网 发布:mac版360浏览器 编辑:程序博客网 时间:2024/05/21 23:25

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;    }};
原创粉丝点击