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 ...
随机推荐
- +load +initialize
+load方法 在app启动的时候各个类的+load方法都会被调用,+load方法不是通过消息机制调用的,它是直接调用的,因此无论是在子类或者category中复写此方法,复写的+load方法都会被调 ...
- 尚硅谷redis学习7-持久化AOF
AOF比RDB优点在于数据的实时性高,经过设置后最多只会损失一秒钟的数据,而RDB最多可能损失上次备份到此次DOWM机间的数据 原理 配置文件 设置同步频率 重启redis,测试,可以看到数据在关机重 ...
- css3实现文本渐变
.gradient-text-one{ background-image:-webkit-linear-gradient(bottom, #00d5fd,#84eaff,#00d5fd); - ...
- 使用DOM的方法获取所有li元素,然后使用jQuery()构造函数把它封装为jQuery对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 注解(Annotation)是什么?
- jQuery中的end()方法
定义和用法 end() 方法结束当前链条中的最近的筛选操作,并将匹配元素集还原为之前的状态. 以上是官方说法,比较难理解. 还是用一个例子来说明 <!DOCTYPE html> <h ...
- Win8系统本地连接显示为网络2
Win8系统中,当改变了网络环境,本地连接就会被识别为网络2,网络3等: 如果在一个固定的网络环境中,需要修改此名称,可以打开注册表: [HKEY_LOCAL_MACHINE\SOFTWARE\Mic ...
- Maven项目之间的关系
1. 依赖关系 1.1 标签<dependency>把另一个项目的jar引入到当前项目 1.2 自动下载另一个项目所依赖的其他项目 2. 继承关系. 2.1 父项目是pom类型 2.2 子 ...
- kotlin集合操作
1.1 总数操作 方法作用: any--判断集合中是否有满足条件 的元素: all--判断集合中的元素是否都满足条件: count--查询集合中满足条件的元素个数: fold--在给定初始值的基础上, ...
- WEB常用前端开发调试工具介绍
只要是设计开发,就需要进行调试,尽管相对来说,前端的调试要简单一些,但使用一些调试工具或插件还是能提高你的工作效率.下面是一些主要用于IE浏览器环境和Firefox浏览器环境等的调试工具简介. 一.I ...