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. 【Python之路】特别篇--微信Web网页版通信的全过程分析

    文章所使用Python版本为py3.5 1.微信服务器返回一个会话ID 微信Web版本不使用用户名和密码直接登录,而是采用二维码登录,所以服务器需要首先分配一个唯一的会话ID,用来标识当前的一次登录. ...

  2. PHP mysqli_fetch_field_direct() 函数

    返回结果集中某个单一字段(列)的 meta-data,并输出字段名称.表格和最大长度: mysqli_fetch_field_direct(result,fieldnr); 参数 描述 result ...

  3. BZOJ 4070: [Apio2015]雅加达的摩天楼 根号分治+spfa

    此题卡Dijkstra... Code: #include <bits/stdc++.h> #define N 30005 #define M 4000000 #define ll lon ...

  4. istio 安装与bookinfo示例运行

    目的 本文旨在帮助想了解istio安装和运行bookinfo示例的同学快速入门 前置准备 安装k8s和helm 1.k8s安装 修改主机名 hostnamectl set-hostname k8s-m ...

  5. 51 Nod 1287 加农炮(单调队列思想+二分)

    1287 加农炮  题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 一个长度为M的正整数数组A,表示从左向右的地形高度 ...

  6. php原生导出简单word表格(TP为例) (原)

      后台: # 菲律宾名单word导出 public function export_word(){ $tids = $_GET['tids']; $userinfo=M("philippi ...

  7. Java-AQS源码详解(细节很多!)

    ReentrantLock调用lock()时时序图: addWaiter方法: enq方法:自旋 它维护了一个volatile int state(代表共享资源)和一个FIFO线程等待队列(多线程争用 ...

  8. QT .pro文件中的变量说明

      https://blog.csdn.net/tanou3212/article/details/79942840 TEMPLATE:定义了工程的编译模式 赋值方式为:TEMPLATE=app (1 ...

  9. 如何用MATLAB GUI创建图形用户界面

    MATLAB是众多理工科学生及工程师经常使用的一款数学软件,除了可以实现数据处理,矩阵运算.函数绘制等功能外,MATLAB还可以实现图形用户界面的设计. 下面介绍如何让小白也能用GUI创建最基本的用户 ...

  10. [go]socket编程

    socket特性 总是成对出现 是全双工的(同时支持收发)(两个channel绑在一起) 应用程序 - cs模式(客户端开发) - bs模式(web开发) net包api基础 都是客户端主动发数据(c ...