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. 解决Spring AOP Controller 不生效

    在spring-mvc.xml文件中,进行以下配置,就可以实现在Controller中, 方法一:最简单的,在spring-mvc.xml配置文件中,添加以下语句 spring-mvc.xml < ...

  2. linux manual free memory

    /proc/sys/vm/drop_caches (since Linux 2.6.16)Writing to this file causes the kernel to drop clean ca ...

  3. Poj 3764 The xor-longest Path(Trie树+xor+贪心)

    The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...

  4. JVM基本讲解

    1.数据类型 java虚拟机中,数据类型可以分为两类:基本类型和引用类型. 基本类型的变量保存原始值,即:它代表的值就是数值本身,而引用类型的变量保存引用值. “引用值”代表了某个对象的引用,而不是对 ...

  5. Postman实现数字签名,Session依赖, 接口依赖, 异步接口结果轮询

    Script(JS)为Postman赋予无限可能 基于Postman 6.1.4 Mac Native版 演示结合user_api_demo实现 PS 最近接到任务, 要把几种基本下单接口调试和持续集 ...

  6. python 生成金字塔

    num = eval(input("请输入一个整数:")) , num + ): , -): print(" ", end="\t") , ...

  7. python国内镜像源

    让python pip使用国内镜像 国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pyp ...

  8. pm2 配置方式

    1.命令生产默认示例配置文件pm2 ecosystem或pm2 init,运行默认会生成ecosystem.config.js配置文件 module.exports = { apps: [ { nam ...

  9. ios-Realm数据库的使用

    [集成 Realm] 本 Demo 使用 OC 创建,所以先进入 Realm 官网 (我记得之前都是有官方中文教程的,但现在最新版没有中文了),到 Objective-C -> Getting ...

  10. elasticsearch+kibana setup

    加载示例数据,设置index的时候出错: 提示 forbidden 则可能是es的问题,需要执行如下命令: curl -XPUT -H "Content-Type: application/ ...