893. Groups of Special-Equivalent Strings 奇数偶数位上的相同数
[抄题]:
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"], ["acb","bca"], ["bac","cab"]
Example 4:
Input: ["abcd","cdab","adcb","cbad"]
Output: 1
Explanation: 1 group ["abcd","cdab","adcb","cbad"]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
多开几个数组就行了
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int numSpecialEquivGroups(String[] A) {
//corner case
if (A == null || A.length == 0) return 0;
//initialization: 2 arrays and a set
Set<String> set = new HashSet<>();
for (String s : A) {
int[] old = new int[26];
int[] even = new int[26];
for (int i = 0; i < s.length(); i++) {
//add to the old or even array
if (i % 2 == 0) {
even[s.charAt(i) - 'a']++;
} else {
old[s.charAt(i) - 'a']++;
}
}
String sig = Arrays.toString(old) + Arrays.toString(even);
set.add(sig);
}
/*
acb
old [0, 0, 1...]
even [1, 1, 0...]
bca
old[0, 0, 1...]
even[1, 1, 0...]
*/
//return
return set.size();
}
}
893. Groups of Special-Equivalent Strings 奇数偶数位上的相同数的更多相关文章
- [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-equiva ...
- day4 对偶数、偶数位的操作
1.函数fun()的功能:从低位开始取出整形变量s中偶数位上的数,依次构成一个新数放在t中.高位仍在高位. 效果理想:但是经测试的时候出现了错误 输入987654321时,打印出来的却是18681.经 ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- [LeetCode] 893. Groups of Special-Equivalent Strings 特殊字符串的群组
You are given an array A of strings. Two strings S and T are special-equivalent if after any number ...
- 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. 1) B. Equivalent Strings DFS暴力
B. Equivalent Strings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559 ...
随机推荐
- springmvc启动时报错:找不到类ContextLoaderListener:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...
- python catch socket timeout
python catch socket timeout import socket try: # do something. except socket.timeout as e: # socket ...
- Linux:条件变量
条件变量: 条件变量本身不是锁!但它也可以造成线程阻塞.通常与互斥锁配合使用.给多线程提供一个会合的场所. 主要应用函数: pthread_cond_init函数 pthrea ...
- 如何使用JBDC修改数据
1.JDBC取得数据库Connection连接对象conn, Connection conn=null; //数据库连接对象 String strSql=null; //sql语句对象 // ...
- 多线程数据库查询(ADO)
ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用(CoInitialize was not called):所以,在使用任何dbGo对象前,必须手 调用CoIniti ...
- serclet监听器
1:监听servlet上下文 2:监听会话 3:监听请求 使用,必须是实现对应的接口,然后在web.xml中配置自己写的监听器的实现类 过滤器之后,servlet之前(有待深入研究) 下一集预告:过滤 ...
- C++ 网络编程 总结
第一次用C++写程序,对C++ 只是菜鸟级别的,倒是对C#很熟悉.两者有很大的相似性.但也有不同. 首先写了一个网络通讯用的小的MFC程序.发现 (1)MFC写界面真的好麻烦呀. 用C#写的tab ...
- C# 图像处理:获取鼠标位置信息(全局)
Point ms = Control.MousePosition; //获取鼠标位置 this.label2.Text = string.Format("{0}:{1}", ms. ...
- 远程批量查看windosws操作系统3389端口的开放情况
本文只提供思想.具体可以根椐情况拓展. 前提是需要配置远程主机的SNMP协议.主要是共同体哟. 脚本使用: 1.拷贝check_tcp到脚本执行的主机中或在此主机中安装nagios; 2.保持list ...
- SQL调用C# dll(第一中DLL,没使用强名称密匙,默认是 safe)
https://msdn.microsoft.com/zh-cn/library/ms345106(es-es).aspx 1.新建项目名称SQLDllTest,类代码如下,没有用Using引用其他类 ...