• Difficulty: Easy

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"], ["abc", "bca"], ["bac", "cab"]

Example 4:

Input: ["abcd", "cdab", "adcb", "cbad"]
Output: 1
Explanation: 3 groups ["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.

Related Topics

String

Solution

统计每个字符串奇数位和偶数位上的字频,当两个字符串以此法统计的字频分布相同时,称这两个字符串为 special-equivalent。我的代码写得似乎麻烦了点,以后补充更简便的写法。

class CharCounter
{
private SortedDictionary<char, int> counter; public CharCounter(string str)
{
counter = new SortedDictionary<char, int>();
bool isOdd = false; foreach(char c in str)
{
if(isOdd)
{
if (counter.ContainsKey(char.ToUpper(c)))
++counter[char.ToUpper(c)];
else
counter.Add(char.ToUpper(c), 1);
}
else
{
if (counter.ContainsKey(c))
++counter[c];
else
counter.Add(c, 1);
}
isOdd = !isOdd;
}
} public override string ToString()
{
StringBuilder builder = new StringBuilder("{");
foreach(var pair in counter)
{
builder.Append($"'{pair.Key}':{pair.Value},");
}
builder.Remove(builder.Length - 1, 1);
builder.Append("}");
return builder.ToString();
} public override bool Equals(object obj)
{
return ToString() == obj.ToString();
} public override int GetHashCode()
{
return ToString().GetHashCode();
}
} public class Solution
{
public int NumSpecialEquivGroups(string[] A)
{
return (from str in A select new CharCounter(str)).ToHashSet().Count;
}
}

[Solution] 893. Groups of Special-Equivalent Strings的更多相关文章

  1. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  2. 893. Groups of Special-Equivalent Strings - LeetCode

    Question 893. Groups of Special-Equivalent Strings Solution 题目大意: AB两个字符串相等的条件是:A中偶数位出现的字符与B中偶数位出现的字 ...

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

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

  4. Equivalent Strings

    Equivalent Strings 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/E 题意: 给出 ...

  5. Codeforces Round #313 (Div. 1) B. Equivalent Strings

    Equivalent Strings Problem's Link: http://codeforces.com/contest/559/problem/B Mean: 给定两个等长串s1,s2,判断 ...

  6. Codeforces Round #313 (Div. 2) D. Equivalent Strings

    D. Equivalent Strings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/ ...

  7. Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力

    B. Equivalent Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559 ...

  8. Equivalent Strings (字符串相等?)

    Equivalent Strings   E - 暴力求解.DFS Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I ...

  9. Codeforces 559B - Equivalent Strings

    559B - Equivalent Strings 思路:字符串处理,分治 不要用substr(),会超时 AC代码: #include<bits/stdc++.h> #include&l ...

随机推荐

  1. React 生命周期及使用场景

    对比版本:16.4.0 VS 16.3.0 VS 16.2.0 发现最近几次React版本更改比较大,在为17.0的大版本作准备.总结了一下React生命周期函数的变化. 综合对比图如下: 各版本分别 ...

  2. [转][SerialPort]测试用例

    private void Form1_Load(object sender, EventArgs e) { var s = SerialPort.GetPortNames().OrderBy(r =& ...

  3. 20175236 2018-2019-2 《Java程序设计》第四周学习总结

    教材学习内容总结 子类与父类 java不支持多重继承,即一个子类不可以从多个父类中同时继承,而C++中可以.人们习惯地称子类与父类的关系式“is—a”的关系. 在类的声明过程中,通过关键字extend ...

  4. 【转】从PowerDesigner概念设计模型(CDM)中的3种实体关系说起

    PowerDesigner概念模型的relationship .inheritance. association 从PowerDesigner概念设计模型(CDM)中的3种实体关系说起

  5. Docker集中化web界面管理平台-Shipyard部署记录

    Docker图形页面管理工具基本常用的有三种: DOCKER UI,Shipyard,Portainer.对比后发现,Shipyard最强大,其次是Portainer,最后是Docker ui.之前介 ...

  6. ftp服务器使用-windowsftp服务起搭建

    首先打开控制面板选择程序 点击启动或关闭windows功能 勾选ftp服务器和IIS管理控制台点击确定 然后右键点击我的电脑,点击管理,然后点击本地用户和组,然后右键点击用户,点击新用户,创建一个用户 ...

  7. 浅谈角色换装功能--Unity简单例子实现

    在前置篇中,基本上梳理了一下换装功能背后涉及到的美术工作流.但程序员嘛,功能终归是要落到代码上的.本文中会结合Unity提供的API及之前提到的内容来实现一个简单的换装功能.效果如下: (图1:最终效 ...

  8. Windows下MongoDB的安装

    1.安装Window2008 R2 并安装 SP1(我安装的是英文版,之前有安装中文版出错) 2.安装vc——redist_x64.exe(不然mongo运行不起来),没安装Sp1前,安装此部会出错. ...

  9. codeblock 生成和使用makefile

    下载cbp2make 文件名:cbp2make-stl-rev138.tar.gz 里面有个cbp文件用codeblock打开,编译,生成的bin目录下有个执行文件. 使用命令生成Makefile . ...

  10. Android开发 解决EditText与NestedScrollView 滚动冲突问题

    问题简述 在将EditText套进NestedScrollView的情况下,EditText输入了多行内容后,无法触摸滚动到第一行 问题原因 在将EditText套进NestedScrollView的 ...