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
.
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的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 【LeetCode】839. 相似字符串组 Similar String Groups (Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,并查集,刷题群 目录 题目描述 解题思路 并查集 代码 刷题心得 欢迎 ...
- 牛客练习赛33 E tokitsukaze and Similar String (字符串哈希hash)
链接:https://ac.nowcoder.com/acm/contest/308/E 来源:牛客网 tokitsukaze and Similar String 时间限制:C/C++ 2秒,其他语 ...
- 牛客练习赛33 E. tokitsukaze and Similar String (字符串哈希)
题目链接:https://ac.nowcoder.com/acm/contest/308/E 题意:中文题 见链接 题解:哈希预处理(三哈希模板) #include <bits/stdc++.h ...
- [LeetCode] Friend Circles 朋友圈
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- [LeetCode] Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- [LeetCode] Redundant Connection 冗余的连接
In this problem, a tree is an undirected graph that is connected and has no cycles. The given input ...
随机推荐
- luogu 2279 [HNOI2003]消防局的设立 树形dp
就是细节多一些,思路都非常常规. Code: #include <bits/stdc++.h> #define N 1005 #define inf 1061109567 #define ...
- [Luogu] 宝藏
https://www.luogu.org/problemnew/show/P3959 模拟退火解法 发现prim求最小生成树是明显错误的 因为prim每次要取出边权最小的点 然而在这道T中这样做不一 ...
- Linux下 Nginx 启动 重启 关闭
命令 nginx -s reload :修改配置后重新加载生效 nginx -s reopen :重新打开日志文件 nginx -t -c /path/to/nginx.conf 测试nginx配置文 ...
- axios拦截器使用方法
vue中axios获取后端接口数据有时候需要在请求开始时显示loading,请求结束后隐藏loading,这时候到每次调接口时都写上有点繁琐,有时候还会漏写. 这时候axios的拦截器就起了作用,我们 ...
- UVA 12501 Bulky process of bulk reduction ——(线段树成段更新)
和普通的线段树不同的是,查询x~y的话,给出的答案是第一个值的一倍加上第二个值的两倍一直到第n个值的n倍. 思路的话,就是关于query和pushup的方法.用一个新的变量sum记录一下这个区间里面按 ...
- node.js获取ip及mac
; (function (win) { var os = require('os'); var ifaces = os.networkInterfaces(); function NetworkUti ...
- Qt 单元测试
使用Qtcreator 自带的单元测试工具框架QTestlib进行测试. 一.创建一个单元测试程序 new project->other project ->Qt unit test ...
- 【MyBatis】【SQL】没有最快,只有更快,从一千万条记录中删除八百万条仅用1分9秒
这次直接使用delete from emp where cdate<'2018-02-02',看看究竟会发生什么. Mapper里写好SQL: <?xml version="1. ...
- git分布式版本管理系统
Git是分布式版本管理系统Svn是集中式版本管理系统 git速度快,适合大规模协同开发 什么是分布式版本管理系统 假如有10个人,每个人的代码库都是独立的,自己想进行代码提交回滚都可以,无需链接中央服 ...
- 在业务控制方法中写入包装User的模型来收集参数
可以在业务控制方法中书写0个或多个模型来收集客户端的参数 1) 如果多个模型中有相同的属性时,可以用user.name或admin.name来收集客户端参数 2) 用一个新的模型将User和Adm ...