zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Linux配置防火墙 开启80端口
    微信支付调用JSAPI缺少参数:timeStamp
    php解压zip文件
    css JavaScript 笔记
    css
    html标签
    模拟服务器返回html
    网页特殊符号HTML代码大全
    selectors模块
    select
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12395004.html
Copyright © 2011-2022 走看看