LeetCode 839. Similar String Groups

原题链接在这里: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 }
复制代码

 

posted @   Dylan_Java_NYC  阅读(306)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
阅读排行:
· 2025成都.NET开发者Connect圆满结束
· 后端思维之高并发处理方案
· 千万级大表的优化技巧
· 在 VS Code 中,一键安装 MCP Server!
· 10年+ .NET Coder 心语 ── 继承的思维:从思维模式到架构设计的深度解析
历史上的今天:
2018-03-02 LeetCode 435. Non-overlapping Intervals
2017-03-02 LeetCode 432. All O`one Data Structure
2016-03-02 LeetCode Super Ugly Number
2016-03-02 LeetCode 298. Binary Tree Longest Consecutive Sequence
点击右上角即可分享
微信分享提示