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 ...
随机推荐
- webpack给目录起别名
1. 配置文件目录: build>webpack.base.config.js: resolve: { alias: { '@': resolve('src'), 'styles': resol ...
- pyqt5的简单进度条程序
# -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/17 16:43 from PyQt5.QtCore import QBasicT ...
- Android学习——MediaProvider与Music模块
一.MediaProvider数据库介绍 1. 关系型数据库 关系模型的物理表示是一个二维表格,由行和列组成. 2. MediaProvider数据库存储位置 /data/data/com.a ...
- mysql的count和sum使用条件表达式
count函数条件不为null的时候显示结果.即使为false也也会显示结果. 可以是使用if条件或者case when语句.如果条件不为null即需要的结果. 使用count()函数实现条件统计的基 ...
- Leetcode题目98.验证二叉搜索树(递归-中等)
题目描述: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是 ...
- centos6安装sshpass
跳转机需要装这个 #!/bin/bash yum -y install gcc-c++ openssh-clients curl -o sshpass.tar.gz http://sourceforg ...
- 【8583】ISO8583各域段的说明
[ISO8583各域段的说明] 1,信息类型(message type)定义位图位置:-格式:定长类型:N4描述:数据包的第一部分,定义数据包的类型.数据类型由数据包的发起者设定,应遵循以下要求:数据 ...
- 异步发送表单数据到JavaBean,并响应JSON文本返回
1) 提交表单后,将JavaBean信息以JSON文本形式返回到浏览器 <form> 编号:<input type="text" name="id&q ...
- SpringMVC整合Springfox-Swagger
https://www.jianshu.com/p/ab10860945c3 验证通过 关于Swagger的简介就不占篇幅了... 本文使用的Springfox-Swagger版本为2.8.0 要整合 ...
- 机器学习之保存与加载.pickle模型文件
import pickle from sklearn.externals import joblib from sklearn.svm import SVC from sklearn import d ...