734. Sentence Similarity

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, “great acting skills” and “fine drama talent” are similar, if the similar word pairs are pairs = [[“great”, “fine”], [“acting”,”drama”], [“skills”,”talent”]].

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

However, similarity is 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”].

这一题简单来说就是用hash table把现有的字典存储起来,然后逐一扫描验证就行。
hash table的key都是unique的,那么如何处理同一个word对应多个近义词就是这题的关键。提供两种解法,分别基于unordered_map和unordered_set.

方法一:unordered_map
用一个key对应一个vector< string>,将所有的近义词存起来就是了。

class Solution {
public:
    bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
        int len = words1.size();
        if (len != words2.size()) return false;
        unordered_map<string, vector<string>> hash;
        for (auto p : pairs) {
            hash[p.first].push_back(p.second);
        }
        for (int i = 0; i < len; i++) {
            if (words1[i] == words2[i]) continue;
            if (hash.find(words1[i]) != hash.end()) {
                int flag = false;
                for (auto s : hash[words1[i]]) {
                    if (words2[i] == s) {
                        flag = true;
                        break;
                    }
                }
                if (flag == true) continue;
            }
            if (hash.find(words2[i]) != hash.end()) {
                int flag = false;
                for (auto s : hash[words2[i]]) {
                    if (words1[i] == s) {
                        flag = true;
                        break;
                    }
                }
                if (flag == true) continue;
            }
            return false;
        }
        return true;
    }
};

方法二: unordered_set
将一对近义词用特殊符合连成一个字符串存起来。

class Solution {
public:
    bool areSentencesSimilar(vector<string>& words1, vector<string>& words2, vector<pair<string, string>> pairs) {
        int len = words1.size();
        if (len != words2.size()) return false;
        unordered_set<string> hash;
        for (auto p : pairs) {
            hash.insert(p.first + "#" + p.second);
        }
        for (int i = 0; i < len; i++) {
            if (words1[i] == words2[i]) continue;
            if (hash.find(words1[i] + "#" + words2[i]) != hash.end()) continue; 
            if (hash.find(words2[i] + "#" + words1[i]) != hash.end()) continue;
            return false;
        }
        return true;
    }
};

这里提一下这种container里常见的insert,emplace以及operator[]的区别。
首先[]和insert是一直都有的。[]是find or add,如果本来就有那个key, 直接返回对应的reference,如果没有,创建一个,作default初始化。insert是如果有,不作为,如果没有,copy or move 来创建一个。
emplace是c++11标准中新加的,跟insert很像,如果有,不作为,不会overload。但是一些时候,emplace 会更加高效,简单来说,因为它在创建一个新的element的时候是in place的,没有copy or move 的操作,通过传递参数完成构建,实际上是dynamic memory allocation。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值