[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 ...
随机推荐
- storm中的一些概念
1.topology 一个topolgy是spouts和bolts组成的图,通过stream groupings将图中的spout和bolts连接起来:如图所示: 一个topology会一直运行知道你 ...
- windows下 go安装qt绑定
1.下载安装QT 离线版QT地址: https://download.qt.io/official_releases/qt/5.11/5.11.1/qt-opensource-windows-x86 ...
- [android]adb 模拟双击 快速点击屏幕
1,记录数据文件到recordtap dd if=/dev/input/event1 of=/sdcard/recordtap 2,点击需要点击的位置,产生点击数据,然后按 ctrl+c 结束 3,写 ...
- 2016-08-15T16:00:00.000Z 格式转换成yyyy-MM-dd HH:mm:ss格式
public static String UTCStringtODefaultString(String UTCString) { try { if (CommonUtils.notNullAndEm ...
- 20164310Exp1 PC平台逆向破解和BOF基础
1.逆向及Bof基础实践说明 1.1实践目标 实践对象:pwn1的linux可执行文件 实践目的:使程序执行另一个代码(ShellCode) 实践内容: 手工修改可执行文件,改变程序执行 ...
- ICE框架双工通讯+MVVM框架测试案例
准备 开发工具 VS2015 ICE框架 https://zeroc.com/ MVVMLight框架 ICE接口文件 #include "./Identity.ice" #inc ...
- 棒槌的工作第八天-----------------------早会-------三次握手四次挥手(转载https://www.cnblogs.com/zmlctt/p/3690998.html)尚待了解,下班补充
TCP三次握手 所谓三次握手(Three-way Handshake),是指建立一个TCP连接时,需要客户端和服务器总共发送3个包. 三次握手的目的是连接服务器指定端口,建立TCP连接,并同步连接双方 ...
- Redis的过期策略和内存淘汰策略
Redis的过期策略:通常有三种,Redis中同时使用惰性过期和定期过期两种过期策略组合. 定时过期:每个设置过期时间的key都需要创建一个定时器,到过期时间就会立即清除.该策略可以立即清除过期的数据 ...
- 75.纯 CSS 创作一支摇曳着烛光的蜡烛
原文地址:https://segmentfault.com/a/1190000015580809 学习后效果地址:https://scrimba.com/c/c8PQ3PTB 感想:CSS 真强大! ...
- Linux如何查看端口
Linux如何查看端口 1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 # lsof -i:8000 COMMAND PID USER ...