ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 839. Similar String Groups

2020-03-02 12:56:37  阅读:329  来源: 互联网

标签:String parent get Similar 839 length similar LeetCode size


原题链接在这里:https://leetcode.com/problems/similar-string-groups/

题目:

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.

题解:

For two strings, if they are similar, put into one union find group.

Eventually, return union find size.

To check if two string are similar, count the chars that they are different, if res > 2, return false.

Time Complexity: O(n^2 * (len+logn)). With path compression and rank by weight, amortized time complexity wouldbe O(n^2*len). n = A.length. len = string length.

Space: O(n*len).

AC Java:

 1 class Solution {
 2     HashMap<String, String> parent = new HashMap<>();
 3     HashMap<String, Integer> size = new HashMap<>();
 4     int count;
 5     
 6     public int numSimilarGroups(String[] A) {
 7         for(String s : A){
 8             parent.put(s, s);
 9             size.put(s, 1);
10         }
11         
12         count = parent.size();
13         
14         int n = A.length;
15         for(int i = 0; i < n; i++){
16             for(int j = i + 1; j < n; j++){
17                 if(A[i].equals(A[j])){
18                     continue;
19                 }
20                 
21                 if(isLegal(A[i], A[j])){
22                     if(!find(A[i], A[j])){
23                         union(A[i], A[j]);
24                     }
25                 }
26             }
27         }
28         
29         return count;
30     }
31     
32     private boolean isLegal(String s, String t){
33         if(s.length() != t.length()){
34             return false;
35         }
36         
37         int res = 0;
38         for(int i = 0; i < s.length(); i++){
39             if(s.charAt(i) != t.charAt(i)){
40                 res++;
41                 if(res > 2){
42                     return false;
43                 }
44             }
45         }
46         
47         return true;
48     }
49     
50     private boolean find(String s, String t){
51         return root(s).equals(root(t));
52     }
53     
54     private String root(String s){
55         if(!s.equals(parent.get(s))){
56             String p = root(parent.get(s));
57             parent.put(s, p);
58         }
59         
60         return parent.get(s);
61     }
62     
63     private void union(String s, String t){
64         String p = root(s);
65         String q = root(t);
66         if(size.get(p) > size.get(q)){
67             size.put(p, size.get(p) + size.get(q));
68             parent.put(q, p);
69         }else{
70             size.put(q, size.get(q) + size.get(p));
71             parent.put(p, q);
72         }
73         
74         count--;
75     }
76 }

 

标签:String,parent,get,Similar,839,length,similar,LeetCode,size
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/12395004.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有