[Solution] 893. Groups of Special-Equivalent Strings
- 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的更多相关文章
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 893. Groups of Special-Equivalent Strings - LeetCode
Question 893. Groups of Special-Equivalent Strings Solution 题目大意: AB两个字符串相等的条件是:A中偶数位出现的字符与B中偶数位出现的字 ...
- 【Leetcode_easy】893. Groups of Special-Equivalent Strings
problem 893. Groups of Special-Equivalent Strings 题意: 感觉参考代码也是有点问题的... 参考 1. Leetcode_easy_893. Grou ...
- Equivalent Strings
Equivalent Strings 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/E 题意: 给出 ...
- Codeforces Round #313 (Div. 1) B. Equivalent Strings
Equivalent Strings Problem's Link: http://codeforces.com/contest/559/problem/B Mean: 给定两个等长串s1,s2,判断 ...
- Codeforces Round #313 (Div. 2) D. Equivalent Strings
D. Equivalent Strings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/ ...
- 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 ...
- Equivalent Strings (字符串相等?)
Equivalent Strings E - 暴力求解.DFS Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I ...
- Codeforces 559B - Equivalent Strings
559B - Equivalent Strings 思路:字符串处理,分治 不要用substr(),会超时 AC代码: #include<bits/stdc++.h> #include&l ...
随机推荐
- Android Studio中绘制simpleUML类图详细说明及使用
一.Android Studio中安装simpleUML 1.下载simpleUML jar包 地址为:http://plugins.jetbrains.com/ 搜索 simpleUMLCE 2. ...
- bzoj5109: [CodePlus 2017]大吉大利,晚上吃鸡!
Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏中,皮皮 和毛毛最喜欢做的事情就是堵桥,每每有一个好时机都能收到不少的快 ...
- Wow64(32位进程)注入DLL到64位进程
转载自: https://blog.poxiao.me/p/wow64-process-inject-dll-into-x64-process/ 向其他进程注入DLL通常的做法是通过调用CreateR ...
- ubuntu 装机步骤表
步骤 1. root 步骤 apt-get update ; apt-get upgrate apt-get install git zsh apt-get install -y make build ...
- 关于eclipse创建web工程没有生成webapp文件夹的解决方案
先看工程建立的是不是配置的打成War包,然后按下图所示
- LinkedHashMap和TreeMap的有序性
做一个数组的多属性动态排序的功能,使用map时发现有序性问题. LinkedHashMap会存储数据的插入顺序,是进入时有序:TreeMap则是默认key升序,是进入后有序(hashMap .hash ...
- Android 开发 View的API 转载
转载地址:https://blog.csdn.net/lemonrabbit1987/article/details/47704679 View类代表用户界面组件的基本构建块.一个View占据屏幕上的 ...
- Exp4 恶意代码分析 20164313 杜桂鑫
1.实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,sys ...
- nginx功能扩展整理
0.基本负载均衡配置 编辑/etc/nginx/nginx.conf,加入负载平衡配置: http { upstream tomcat { server localhost:8080; server ...
- orcal - 添加用户、授权
create user jy2 identified by jy2; grant dba to jy2;