返回介绍

solution / 0700-0799 / 0734.Sentence Similarity / README_EN

发布于 2024-06-17 01:03:35 字数 5805 浏览 0 评论 0 收藏 0

734. Sentence Similarity

中文文档

Description

We can represent a sentence as an array of words, for example, the sentence "I am happy with leetcode" can be represented as arr = ["I","am",happy","with","leetcode"].

Given two sentences sentence1 and sentence2 each represented as a string array and given an array of string pairs similarPairs where similarPairs[i] = [xi, yi] indicates that the two words xi and yi are similar.

Return _true if sentence1 and sentence2 are similar, or false if they are not similar_.

Two sentences are similar if:

  • They have the same length (i.e., the same number of words)
  • sentence1[i] and sentence2[i] are similar.

Notice that a word is always similar to itself, also notice that the similarity relation is not transitive. For example, if the words a and b are similar, and the words b and c are similar, a and c are not necessarily similar.

 

Example 1:

Input: sentence1 = ["great","acting","skills"], sentence2 = ["fine","drama","talent"], similarPairs = [["great","fine"],["drama","acting"],["skills","talent"]]
Output: true
Explanation: The two sentences have the same length and each word i of sentence1 is also similar to the corresponding word in sentence2.

Example 2:

Input: sentence1 = ["great"], sentence2 = ["great"], similarPairs = []
Output: true
Explanation: A word is similar to itself.

Example 3:

Input: sentence1 = ["great"], sentence2 = ["doubleplus","good"], similarPairs = [["great","doubleplus"]]
Output: false
Explanation: As they don't have the same length, we return false.

 

Constraints:

  • 1 <= sentence1.length, sentence2.length <= 1000
  • 1 <= sentence1[i].length, sentence2[i].length <= 20
  • sentence1[i] and sentence2[i] consist of English letters.
  • 0 <= similarPairs.length <= 1000
  • similarPairs[i].length == 2
  • 1 <= xi.length, yi.length <= 20
  • xi and yi consist of lower-case and upper-case English letters.
  • All the pairs (xi, yi) are distinct.

Solutions

Solution 1

class Solution:
  def areSentencesSimilar(
    self, sentence1: List[str], sentence2: List[str], similarPairs: List[List[str]]
  ) -> bool:
    if len(sentence1) != len(sentence2):
      return False
    s = {(a, b) for a, b in similarPairs}
    return all(
      a == b or (a, b) in s or (b, a) in s for a, b in zip(sentence1, sentence2)
    )
class Solution {
  public boolean areSentencesSimilar(
    String[] sentence1, String[] sentence2, List<List<String>> similarPairs) {
    if (sentence1.length != sentence2.length) {
      return false;
    }
    Set<String> s = new HashSet<>();
    for (List<String> e : similarPairs) {
      s.add(e.get(0) + "." + e.get(1));
    }
    for (int i = 0; i < sentence1.length; ++i) {
      String a = sentence1[i], b = sentence2[i];
      if (!a.equals(b) && !s.contains(a + "." + b) && !s.contains(b + "." + a)) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool areSentencesSimilar(vector<string>& sentence1, vector<string>& sentence2, vector<vector<string>>& similarPairs) {
    int m = sentence1.size(), n = sentence2.size();
    if (m != n) return false;
    unordered_set<string> s;
    for (auto e : similarPairs) s.insert(e[0] + "." + e[1]);
    for (int i = 0; i < n; ++i) {
      string a = sentence1[i], b = sentence2[i];
      if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false;
    }
    return true;
  }
};
func areSentencesSimilar(sentence1 []string, sentence2 []string, similarPairs [][]string) bool {
  if len(sentence1) != len(sentence2) {
    return false
  }
  s := map[string]bool{}
  for _, e := range similarPairs {
    s[e[0]+"."+e[1]] = true
  }
  for i, a := range sentence1 {
    b := sentence2[i]
    if a != b && !s[a+"."+b] && !s[b+"."+a] {
      return false
    }
  }
  return true
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文