[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 ...
随机推荐
- win10.64位wnmp-nginx1.14.0 + PHP 5. 6.36 + MySQL 5.5.59 环境配置搭建 结合Thinkphp3.2.3
本文20%是原创,另外参考了这里https://blog.csdn.net/foolly/article/details/78963025 作者:CSDN 古雨蓝枫 和这里https://www.cn ...
- win10家庭版,双击bat文件无法运行(double click bat file does not execute)
win10家庭版,双击bat文件无法运行,弹出文件打开方式选择框. 在网上搜索处理办法,试了以下方法1-5都没有成功,用方法6规避. 方法1:打开一个驱动器,点“工具-文件夹选项→文件类型→新建→扩展 ...
- eclipse--常见问题
学习java的都知道这个编辑器,但这个编辑器的有很多功能很多人不知道怎么用,我系统的整理一下我学习过程中遇到的过的问题 1.eclipse如何导入external jar包? 参考:ht ...
- hanlp使用自定义词典抽取关键词
1.在data/dictionary/custom/路径下新建文件 myDict.txt.,添加新的单词,单词,词性,词频.并删除当前文件夹下的bin文件, 2.在hanlp配置文件中的CustomD ...
- gradle指定相应JDK编译
转载请注明出处: http://blog.csdn.net/sanyinchen/article/details/50901582 问题描述: 电脑中装有多个jdk版本,可能默认的jdk是1.6,但是 ...
- ThreadLocal 原理及一些实现
ThreadLocal = TL 网上讲TL原理很多,我大概说下自己的理解 TL其实是不是有点像全局的配置中心,static ConcurrentHashMap<Thread,value> ...
- mybatis学习 -每天一记 mybatis insert null 报错
mybatis 插入数据,model的属性存在null,插入报错 在使用mybatis 进行insert时,如果字段值存在null的情况,会出现插入失败的情况,解决方案: 如果使用spring boo ...
- 嵌入式linux——汇编、C语言基础(一)
一.汇编语言基础 (断断续续的记录自己的笔记...2018-10-11) 1. mov指令 基本用法: mov r0, #0 mov指令是赋值指令,用法如上,把立即数0放入到寄存器r0中. 2. bl ...
- cached_property的使用
cached_property修饰过的函数,变成是对象的属性,该对象第一次引用该属性时,会调用函数,对象第二次引用该属性时就直接从词典中取了,这也说明引用属性是经过__getattritue__. c ...
- Unable to locate appropriate constructor on class报错
在项目开发中,使用Hibernate里的JPA criteria查询,但是在写完之后使用时,会报错:Unable to locate appropriate constructor on class, ...