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:

  1. Input: ["tars","rats","arts","star"]
  2. Output: 2
    分析:
    因为这个是找group,所以容易联想到用union-find.这里我们在确定谁是root的时候,总是挑index大的,这样才能把不同的groups合并成一个group.
  1. public class Solution {
  2. public int numSimilarGroups(String[] A) {
  3. int[] roots = new int[A.length];
  4. for (int i = ; i < A.length; i++) {
  5. roots[i] = i;
  6. }
  7. for (int i = ; i < A.length; i++) {
  8. for (int j = ; j < i; j++) {
  9. if (similar(A[i], A[j])) {
  10. roots[find(roots, j)] = i;
  11. }
  12. }
  13. }
  14. int result = ;
  15. for (int i = ; i < roots.length; i++) {
  16. if (roots[i] == i) {
  17. result++;
  18. }
  19. }
  20. return result;
  21. }
  22. private int find(int[] roots, int id) {
  23. while (roots[id] != id) {
  24. roots[id] = roots[roots[id]]; // path compression
  25. id = roots[id];
  26. }
  27. return id;
  28. }
  29. private boolean similar(String a, String b) {
  30. int res = , i = ;
  31. boolean consecutive = false;
  32. while (res <= && i < a.length()){
  33. if (a.charAt(i) != b.charAt(i)) {
  34. res++;
  35. }
  36. if (i > && a.charAt(i) == a.charAt(i - )) {
  37. consecutive = true;
  38. }
  39. i++;
  40. }
  41. return res == || res == && consecutive;
  42. }
  43. }

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. webpack给目录起别名

    1. 配置文件目录: build>webpack.base.config.js: resolve: { alias: { '@': resolve('src'), 'styles': resol ...

  2. pyqt5的简单进度条程序

    # -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/5/17 16:43 from PyQt5.QtCore import QBasicT ...

  3. Android学习——MediaProvider与Music模块

    一.MediaProvider数据库介绍 1. 关系型数据库 关系模型的物理表示是一个二维表格,由行和列组成. 2. MediaProvider数据库存储位置     /data/data/com.a ...

  4. mysql的count和sum使用条件表达式

    count函数条件不为null的时候显示结果.即使为false也也会显示结果. 可以是使用if条件或者case when语句.如果条件不为null即需要的结果. 使用count()函数实现条件统计的基 ...

  5. Leetcode题目98.验证二叉搜索树(递归-中等)

    题目描述: 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是 ...

  6. centos6安装sshpass

    跳转机需要装这个 #!/bin/bash yum -y install gcc-c++ openssh-clients curl -o sshpass.tar.gz http://sourceforg ...

  7. 【8583】ISO8583各域段的说明

    [ISO8583各域段的说明] 1,信息类型(message type)定义位图位置:-格式:定长类型:N4描述:数据包的第一部分,定义数据包的类型.数据类型由数据包的发起者设定,应遵循以下要求:数据 ...

  8. 异步发送表单数据到JavaBean,并响应JSON文本返回

    1)  提交表单后,将JavaBean信息以JSON文本形式返回到浏览器 <form> 编号:<input type="text" name="id&q ...

  9. SpringMVC整合Springfox-Swagger

    https://www.jianshu.com/p/ab10860945c3 验证通过 关于Swagger的简介就不占篇幅了... 本文使用的Springfox-Swagger版本为2.8.0 要整合 ...

  10. 机器学习之保存与加载.pickle模型文件

    import pickle from sklearn.externals import joblib from sklearn.svm import SVC from sklearn import d ...