博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 839. Similar String Groups 并查集
阅读量:4186 次
发布时间:2019-05-26

本文共 3369 字,大约阅读时间需要 11 分钟。

Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y. Also two strings X and Y are similar if they are equal.

For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars""rats", or "arts".

Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}.  Notice that "tars" and "arts" are in the same group even though they are not similar.  Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.

We are given a list A of strings.  Every string in A is an anagram of every other string in A.  How many groups are there?

 

Example 1:

Input: A = ["tars","rats","arts","star"]Output: 2

 

Constraints:

  • 1 <= A.length <= 2000
  • 1 <= A[i].length <= 1000
  • A.length * A[i].length <= 20000
  • All words in A consist of lowercase letters only.
  • All words in A have the same length and are anagrams of each other.
  • The judging time limit has been increased for this question.

Accepted

15,146

Submissions

41,209

---------------------------------------------------------------------------------------------

几个注意点:

  1. 用并查集数有几组,最多需要两两都比较一遍。例如A和B一组,A和C一组,那么B和C一组。但是对于这题来说,A和B不相似,A和C相似,B和C相似,那么A和B也一组,所以两两比较逃不掉。最多能避免的情况就是A和B相似,A和C相似,那么B和C肯定一组不用判断了。但是这也取决于B,C提前在一组的可能性,实测并没有变快
  2. 对于Python这种慢语言,这题必须两种角度考虑:直接比、neighbor+字典
  3. 对于Python这种慢语言,长度为w的字符串,求一个neighbor的复杂度是O(w),全部Neighbor就是O(w^3),所以比较的时候要注意

最后是codes:

class Solution:    def isSimilar(self, s1, s2):        diff, l = 0, len(s1)        for i in range(l):            if (s1[i] != s2[i]):                diff += 1                if (diff > 2):                    return False        return True    def find(self, f, x):        return f[x] if x == f[x] else self.find(f, f[x])    def merge(self, f, x, y):        rx = self.find(f, f[x])        ry = self.find(f, f[y])        f[ry] = rx    def numSimilarGroups(self, A):        A = list(set(A))        l,w = len(A), len(A[0])        res = 0        f = [i for i in range(l)]        if l <= w*w:            for i in range(l):                for j in range(i + 1, l):                    if (self.find(f, i) != self.find(f,j)):                        isSimilar = self.isSimilar(A[i], A[j])                        if (isSimilar):                            self.merge(f, i, j)        else:            dict = {}            for i in range(l):                if (A[i] in dict):                    dict[A[i]].add(i)                else:                    dict[A[i]] = {i}                word = list(A[i])                for i0 in range(w):                    for j0 in range(i0+1, w):                        if (word[i0] != word[j0]):                            word[i0],word[j0] = word[j0],word[i0]                            neighbor = ''.join(word)                            if (neighbor in dict):                                dict[neighbor].add(i)                            else:                                dict[neighbor] = {i}                            word[i0],word[j0] = word[j0],word[i0]            for i in range(l):                for j in dict[A[i]]:                    self.merge(f,i,j)        for i in range(l):            if (i == f[i]):                res += 1        return ress = Solution()print(s.numSimilarGroups(["tars","rats","arts","star"]))

 

转载地址:http://ptfoi.baihongyu.com/

你可能感兴趣的文章
mondrian和ssas哪个好
查看>>
Solr作为一个Web应用,可以部署在多种应用服务器
查看>>
Directory家族的层级分布图
查看>>
我们为什么应该坚持写博客
查看>>
因为多个jar可能记录日志信息时,日志模块,不知道需要用那个jar包
查看>>
Lucene里面支持join操作
查看>>
solr 4.2的入门配置
查看>>
shell脚本一键安装solr
查看>>
solr原子更新功能
查看>>
greenplum + pgsql和Hadoop+hive+hbase
查看>>
cpu,硬盘,内存
查看>>
Elasticsearch 合理内存分配
查看>>
elasticsearch 与 hive集成
查看>>
ElasticSearch 2 的节点调优(ElasticSearch性能)
查看>>
Elasticsearch与hadoop比较
查看>>
raid5
查看>>
eclipse中调试solr
查看>>
solr有关Pig0.12.0和Solr4.10.2
查看>>
建议使用Solr或ElasticSearch这样的封装了
查看>>
图形数据库Neo4J简介
查看>>