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

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: ["tars","rats","arts","star"]
Output: 2
分析:
因为这个是找group,所以容易联想到用union-find.这里我们在确定谁是root的时候,总是挑index大的,这样才能把不同的groups合并成一个group.
 public class Solution {
public int numSimilarGroups(String[] A) {
int[] roots = new int[A.length];
for (int i = ; i < A.length; i++) {
roots[i] = i;
}
for (int i = ; i < A.length; i++) {
for (int j = ; j < i; j++) {
if (similar(A[i], A[j])) {
roots[find(roots, j)] = i;
}
}
}
int result = ;
for (int i = ; i < roots.length; i++) {
if (roots[i] == i) {
result++;
}
}
return result;
}
private int find(int[] roots, int id) {
while (roots[id] != id) {
roots[id] = roots[roots[id]]; // path compression
id = roots[id];
}
return id;
}
private boolean similar(String a, String b) {
int res = , i = ;
boolean consecutive = false;
while (res <= && i < a.length()){
if (a.charAt(i) != b.charAt(i)) {
res++;
}
if (i > && a.charAt(i) == a.charAt(i - )) {
consecutive = true;
}
i++;
}
return res == || res == && consecutive;
}
}

Similar String Groups的更多相关文章

  1. [Swift]LeetCode839. 相似字符串组 | Similar String Groups

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

  2. [LeetCode] 839. Similar String Groups 相似字符串组

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

  3. leetcode 839 Similar String Groups

    题目 Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that ...

  4. 【LeetCode】839. 相似字符串组 Similar String Groups (Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,并查集,刷题群 目录 题目描述 解题思路 并查集 代码 刷题心得 欢迎 ...

  5. 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)

    链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...

  6. 牛客练习赛33 E. tokitsukaze and Similar String (字符串哈希)

    题目链接:https://ac.nowcoder.com/acm/contest/308/E 题意:中文题 见链接 题解:哈希预处理(三哈希模板) #include <bits/stdc++.h ...

  7. [LeetCode] Friend Circles 朋友圈

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  8. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  9. [LeetCode] Redundant Connection 冗余的连接

    In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...

随机推荐

  1. Codeforces 1254C/1255F Point Ordering (交互题)

    题目链接 http://codeforces.com/contest/1254/problem/C 题解 sb题. 第一次,通过\((n-2)\)次询问2确定\(p[2]\),也就是从\(1\)来看& ...

  2. 9.Spring Cloud Config统一管理微服务配置

    Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...

  3. django快速实现完整登录系统,把登陆注册串在一起并增加cookie(六)

    1.使用之前创建的项目和应用  mysite3 account 2.使用之前的数据库构造 class User(models.Model): username=models.CharField(max ...

  4. 黑马vue---20、v-if和v-show的使用和特点

    黑马vue---20.v-if和v-show的使用和特点 一.总结 一句话总结: v-if 的特点:每次都会重新删除或创建元素 v-show 的特点: 每次不会重新进行DOM的删除和创建操作,只是切换 ...

  5. HearthBuddy炉石兄弟 如何调试ai

    Sepefeets's update to botmaker's Silverfish AI This AI is a Custom Class for Hearthranger and Hearth ...

  6. How to get full path of StreamWriter

     How to get full path of StreamWriter   In my version of the framework, this seems to work: string f ...

  7. 查找与排序算法(Searching adn Sorting)

    1,查找算法 常用的查找算法包括顺序查找,二分查找和哈希查找. 1.1 顺序查找(Sequential search) 顺序查找: 依次遍历列表中每一个元素,查看是否为目标元素.python实现代码如 ...

  8. 1.分布式配置中心 spring-cloud-config

    pring Cloud 版本:2.1.0.RELEASE 一.server端 1.maven依赖 <dependency> <groupId>org.springframewo ...

  9. thinkphp5的控制器调用自身模块和调用其他模块的方法

    以user为例,调用user.php的get_number()方法 一.不管是调用自身模块还是其他模块app\model\User.php写法不变 <?php namespace app\ind ...

  10. sparkstreaming的状态计算-updateStateByKey源码

    转发请注明原创地址:https://www.cnblogs.com/dongxiao-yang/p/11358781.html 本文基于spark源码版本为2.4.3 在流式计算中通常会有状态计算的需 ...