Algorithm

【leetcode】893. Groups of Special-Equivalent Strings

https://leetcode.com/problems/groups-of-special-equivalent-strings/

1)problem

You are given an array A of strings.

Two strings S and T are special-equivalent if after any number of moves, S == T.

A move consists of choosing two indices i and j with i % 2 == j % 2, and swapping S[i] with S[j].

Now, a group of special-equivalent strings from A is a non-empty subset S of A such that any string not in S is not special-equivalent with any string in S.

Return the number of groups of special-equivalent strings from A.

Example 1:

Input: ["a","b","c","a","c","c"]
Output: 3
Explanation: 3 groups ["a","a"], ["b"], ["c","c","c"]

Example 2:

Input: ["aa","bb","ab","ba"]
Output: 4
Explanation: 4 groups ["aa"], ["bb"], ["ab"], ["ba"]

Example 3:

Input: ["abc","acb","bac","bca","cab","cba"]
Output: 3
Explanation: 3 groups ["abc","cba"], ["acb","bca"], ["bac","cab"]

Example 4:

Input: ["abcd","cdab","adcb","cbad"]
Output: 1
Explanation: 1 group ["abcd","cdab","adcb","cbad"]

Note:

  • 1 <= A.length <= 1000
  • 1 <= A[i].length <= 20
  • All A[i] have the same length.
  • All A[i] consist of only lowercase letters.

2)answer

统计奇数位和偶数位的个字符出现的次数,如果 S0 串和 S1 串统计的结果相同,则它们是 special-equivalent 的。

3)solution

  1. 将字符串拆分为两个子字符串,1个包含偶数索引字符,1个包含奇数字符串
  2. 对两个子字符串进行排序(这样做是因为如果可以将字符串与另一个字符串交换,那么在排序时它们将彼此相等,因为它们必须具有相同的字符)
  3. 将一对字符串插入集合中,这将跟踪唯一的“组”
  4. 重新调整集合的大小

符合上面两个条件的是一组。

参考:https://blog.csdn.net/g_r_c/article/details/82079678

unordered_set使用方法

.find() 返回一个迭代器。这个迭代器指向和参数哈希值匹配的元素,如果没有匹配的元素,会返回这个容器的结束迭代器。
.end() 返回指向容器末尾位置的迭代器
.insert() 插入元素 

视频:

https://www.youtube.com/watch?v=WJ4NtyrakT0&feature=youtu.be

图片示例:

C++实现代码:

#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
using std::vector;
using std::string;
using std::unordered_set;

class Solution {
public:
    int numSpecialEquivGroups(vector<string>& A) {
        unordered_set<string> st;
        for (int i = 0; i < A.size(); ++i) {
            string odd = "";
            string even = "";
            for (int j = 0; j < A[i].size(); ++j) {
                // 奇偶位的值
                if (j % 2 == 0)
                    odd += A[i][j];
                else
                    even += A[i][j];
            }
            // 排序奇偶位的值
            sort(odd.begin(), odd.end());
            sort(even.begin(), even.end());
            st.insert(odd + even);

        }
        return st.size();
    }
};
int main()
{
    Solution solution;
    vector<string> A1 = { "abcd", "cdab", "adcb", "cbad" };
    vector<string> A2 = { "abc", "acb", "bac", "bca", "cab", "cba" };
    int res1 = solution.numSpecialEquivGroups(A1);
    int res2 = solution.numSpecialEquivGroups(A2);
}

Python实现:

def numSpecialEquivGroups(self,A):
    s = set()
    for w in A:
        even = ''.join(sorted(w[0::2]))
        odd  = ''.join(sorted(w[1::2]))
        s.add(odd+even)
    return len(s)

【leetcode】893. Groups of Special-Equivalent Strings的更多相关文章

  1. 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【Leetcode_easy】893. Groups of Special-Equivalent Strings

    problem 893. Groups of Special-Equivalent Strings 题意: 感觉参考代码也是有点问题的... 参考 1. Leetcode_easy_893. Grou ...

  3. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  4. 【LeetCode】5685. 交替合并字符串 Merge Strings Alternately (Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,交替合并字符串,Merge Strings Alternately,刷题群 目 ...

  5. 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【Leetcode】583. Delete Operation for Two Strings

    583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of st ...

  7. 【leetcode】1247. Minimum Swaps to Make Strings Equal

    题目如下: You are given two strings s1 and s2 of equal length consisting of letters "x" and &q ...

  8. 【leetcode】1071. Greatest Common Divisor of Strings

    题目如下: For strings S and T, we say "T divides S" if and only if S = T + ... + T  (T concate ...

  9. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

随机推荐

  1. 2017-12-14python全栈9期第一天第五节之变量、常量、注释

    6,变量. 变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用. 1,必须由数字,字母,下划线任意组合,且不能数字开头. 2,不能是python中的关键字. ['and', 'as', 'a ...

  2. urllib 学习二

    编码解码: python2 用法: urllib.urlencode() 编码 urlparse.parse_qs() 解码 python3 用法: urllib.parse.urlencode() ...

  3. 编译Hadoop 2.7.2支持压缩 转

    hadoop Native Shared Libraries 使得Hadoop可以使用多种压缩编码算法,来提高数据的io处理性能.不同的压缩库需要依赖到很多Linux本地共享库文件,社区提供的二进制安 ...

  4. 设计模式---接口隔离模式之中介者模式(Mediator)

    一:概念 在Mediator模式中,类之间的交互行为被统一放在Mediator的对象中,对象通过Mediator对象同其他对象交互.Mediator对象起到控制器的作用 二:动机 在软件构建的过程中, ...

  5. Linux记录-shell实现脚本监控服务器及web应用

    1.apache web 服务器 1 2 3 4 5 6 7 8 9 10 !/bin/bash # 表示请求链接3秒钟,不要返回的测试数据 nc -w 3 localhost 80 &> ...

  6. Python复习笔记(八)迭代器和生成器和协程

    1. 迭代器 1.1 可迭代对象 判断xxx_obj是否可以迭代 在第1步成立的前提下,调用 iter 函数得到 xxx_obj 对象的 __iter__ 方法的返回值 __iter__ 方法的返回值 ...

  7. 细说logback之简介

    官网:https://logback.qos.ch/https://logback.qos.ch/manual/index.html logback手册1.下载logback是slf4j的原生实现,所 ...

  8. C# 获取当前路径方法整理

    https://www.cnblogs.com/tianma3798/p/6553863.html1. //获取包含清单的已加载文件的路径或 UNC 位置. public static string ...

  9. JDK8新特性04 方法引用与构造器引用

    import java.io.PrintStream; import java.util.Comparator; import java.util.function.*; /** * 一.方法引用 * ...

  10. linux 下shell脚本备份文件

    以下是shell自动备份用的: 主要功能: 1)将pathSrc目录中的文件拷贝到pathDst目录中去. 具体步骤:先查询源目录和目标目录中的文件,分别存在fileSrc和fileDst中. -&g ...