LeetCode 893. Groups of Special-Equivalent Strings (特殊等价字符串组)
题目标签:String
题目可以让在 偶数位置的 chars 互换, 也可以让 在 奇数位置的 chars 互换。
所以为了 return 正确的 group 数量,需要把 那些重复的 给排除掉。
可以把在 偶数位置的 chars 都拿出来 组成一个 string a, 同样的把 在奇数位置上的 chars 都拿出来组成一个 string b,分别把a 和 b 排序一下,再把两个a 和 b组合并且存入 HashSet。
最后只要返回 HashSet 的 size 就可以了。
Java Solution:
Runtime beats 74.65%
完成日期:10/24/2018
关键点:把偶数位置 和 奇数位置的 chars 分别组成string,再排序,再组成新的String
class Solution
{
public int numSpecialEquivGroups(String[] A)
{
Set<String> seen = new HashSet<>(); for(String str: A)
{
String evenStr = "";
String oddStr = ""; for(int i = 0; i < str.length(); i++)
{
if(i % 2 == 0) // even index
{
evenStr += str.charAt(i);
}
else // odd index
{
oddStr += str.charAt(i);
}
} seen.add(sortString(evenStr) + sortString(oddStr)); } return seen.size();
} private String sortString(String s)
{
char[] arr = s.toCharArray();
Arrays.sort(arr); return new String(arr);
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 893. Groups of Special-Equivalent Strings (特殊等价字符串组)的更多相关文章
- [Swift]LeetCode893. 特殊等价字符串组 | 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 ...
- [LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作
Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...
- [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 ...
- 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 nu ...
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
- Codeforces 559B - Equivalent Strings
559B - Equivalent Strings 思路:字符串处理,分治 不要用substr(),会超时 AC代码: #include<bits/stdc++.h> #include&l ...
- [LeetCode] 712. Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和
Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...
- 【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中偶数位出现的字 ...
随机推荐
- HTML5 页面调用微信接口
参考微信微信官方文档JS_SDK:https://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#JSSDK.E4.BD.B ...
- PHP引用(&)的考察点
引用的概念 在PHP中引用意味着用不同的名字访问同一个变量内容. 定义方式 使用 & 符号来表示 变量的引用 $a = 'ABC'; //开辟一块内存空间存储数据,$a指向该空间 $b = & ...
- 再谈布局之 UIStackView
UIStackView 是 iOS9 新增的一个布局技术.熟练掌握相当节省布局时间. UIStackView 是 UIView 的子类,是用来约束子控件的一个控件.但他的作用仅限于此,他不能被渲染(即 ...
- 机器学习_K近邻Python代码详解
k近邻优点:精度高.对异常值不敏感.无数据输入假定:k近邻缺点:计算复杂度高.空间复杂度高 import numpy as npimport operatorfrom os import listdi ...
- org-table
Table of Contents 1. table 1.1. 创建方式 1.2. 重新对齐 1.3. 行列编辑 1.4. 区域 1.5. 计算 1.6. 其他的 1.7. 行宽度 1.8. 列分 ...
- SwiftyUserDefaults对NSUserDefaults的封装扩展
SwiftyUserDefaults 是对NSUserDefaults的一些封装和扩展,这个库这个只有一个类,操作起来十分简单方便: 这里只有两个步骤来使用SwiftyUserDefaults: st ...
- [Luogu] P3258 [JLOI2014]松鼠的新家
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...
- Windows Server 2008R2服务器IIS安装步骤
注意点: 添加ASP.NET ..NET 扩展性.CGI.ISAPI 扩展.ISAPI 筛选器,去掉 目录浏览(因为大多数网站用不到.) 如果需要用到asp则勾选asp,如果需要用shtm需要开启在服 ...
- 【Codeforces 1038D】Slime
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 相当于让你确定每个数字前面的系数是-1还是+1 有个结论是这样每次和相邻的减的话, 任何出除了全"-1"和全"+ ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...