成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode/LintCode] Sentence Similarity

dreamtecher / 2881人閱讀

Problem

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"].

Solution
public class Solution {
    /**
     * @param words1: a list of string
     * @param words2: a list of string
     * @param pairs: a list of string pairs
     * @return: return a boolean, denote whether two sentences are similar or not
     */
    public boolean isSentenceSimilarity(String[] words1, String[] words2, List> pairs) {
        // write your code here
        if (words1.length != words2.length) return false;
        Map> map = new HashMap<>();
        for (List pair: pairs) {
            if (!map.containsKey(pair.get(0))) {
                map.put(pair.get(0), new HashSet());
            }
            if (!map.containsKey(pair.get(1))) {
                map.put(pair.get(1), new HashSet());
            }
            map.get(pair.get(0)).add(pair.get(1));
            map.get(pair.get(1)).add(pair.get(0));
        }
        for (int i = 0; i < words1.length; i++) {
            // if (words1[i] == words2[i]) continue;
            if (!map.containsKey(words1[i])) return false;
            if (!map.get(words1[i]).contains(words2[i])) return false;
        }
        return true;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71632.html

相關(guān)文章

  • 【手撕 - 自然語言處理】手撕 TextRank(01)大佬是怎么實現(xiàn) Python 版的

    摘要:開撕文件夾下的程序展示了怎么使用這個版本的。文件行數(shù)這句是重點摘要然后,我們知道重點函數(shù)是,我們再來看它是怎么工作的。再仔細(xì)閱讀一遍,原來寫這個庫的大佬用種不同的方法實現(xiàn)了個函數(shù),請收下我的膝蓋。 作者:LogM 本文原載于 https://segmentfault.com/u/logm/articles ,不允許轉(zhuǎn)載~ 1. 源碼來源 TextRank4ZH 源碼:https://g...

    JerryC 評論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評論0 收藏0
  • [LeetCode/LintCode] Is Subsequence

    Problem Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) ...

    terasum 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<