• 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. 【译】Optaplanner开发手册本地化: (0) - 前言及概念

    在此之前,针对APS写了一些理论性的文章:而对于Optaplanner也写了一些介绍性质,几少量入门级的帮助初学者走近Optaplanner.在此以后,老农将会按照Optaplanner官方的用户手册 ...

  2. 判断文件是否存在,不要用if exist和if not exist,因为他们会受到文件是否隐藏的影响,改用dir /a 命令代替

    @echo off & setlocal enabledelayedexpansionrem 判断文件是否存在,不要用if exist和if not exist,因为他们会受到文件是否隐藏的影 ...

  3. bzoj5108: [CodePlus2017]可做题

    Description qmqmqm希望给sublinekelzrip出一道可做题.于是他想到了这么一道题目:给一个长度为n的非负整数序列ai,你需 要计算其异或前缀和bi,满足条件b1=a1,bi= ...

  4. 级联Cascade

    cascade分为两类: 1.JPA标准 2.Hibernate标准 JPA的方法使用JPA规范 如@OneToMany(cascade=CascadeType.ALL,mappedBy=" ...

  5. pandas学习笔记(一)

    Pandas是一款开放源码的BSD许可的Python库,为Python编程语言提供了高性能,易于使用的数据结构和数据分析工具.Pandas用于广泛的领域,包括金融,经济,统计,分析等学术和商业领域.在 ...

  6. Java Base64位加密和解密(包括其他加密参考)

    链接https://blog.csdn.net/longguangfu8/article/details/78948213 常用加密解密算法[RSA.AES.DES.MD5]介绍和使用 https:/ ...

  7. pip升级

    只要出现报错:python -m pip install --upgrade pip.都表示需要进行升级pip版本 查看pip版本:pip -V(pip可能是python2版本或python3版本) ...

  8. 【Python】爬虫

    参考:http://www.cnblogs.com/fnng/p/3576154.html import urllib,re page = urllib.urlopen('http://trend.b ...

  9. !!常用HTML5代码

    HTML5提供的新特性 2016-2-16 Web Socket 定义了一套API, 允许网页能够使用Web Socket协议来和远程主机进行双工通信. Web Storage 定义了一套API, 能 ...

  10. FPGA——入手(零)

    前几天正点原子团队退出了FPGA开发板,我就买了一套.我想的是,多学一点东西,即使到最后积累下的东西少,但是面是广的,以后可以更好的选择一种深入.就入手了新起点FPGA开发板,新起点算是开拓者的阉割版 ...