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中偶数位出现的字 ...
随机推荐
- ReLU激活函数:简单之美
出自 http://blog.csdn.net/cherrylvlei/article/details/53149381 导语 在深度神经网络中,通常使用一种叫修正线性单元(Rectified lin ...
- jsp学习笔记 - 内置对象 application
---恢复内容开始--- 1.application一般用this.getServletContext()替代 2.appllication有一个非常有用的函数 getRealPath(),获取绝对路 ...
- CAD把控件图形变成二进制流输出(com接口VB语言)
主要用到函数说明: _DMxDrawX::WriteBinStream 把控件图形变成二进制流输出,详细说明如下: 参数 说明 VARIANT* pVarBinArray 输出二进制数组.是个byte ...
- "ping: unknown host www.baidu.com"问题解决方式
参考:https://blog.csdn.net/wbainngg123/article/details/51540535 在虚拟机VMware里选择桥接模式,并配置网络之后,发现ping ip地址可 ...
- PHP--选择排序
<?php /** * 选择排序(从小到大)的思想:每一次从待排序的数据中选出最小的,放在待排序的起始位置. */ $arr = array(23, 42, 21, 8, 4, 2, 3, 1) ...
- linux下C/C++程序的内存布局
内核空间和用户空间: 我们在编写程序时使用的内存空间叫虚拟内存,程序在运行时,要完成虚拟内存到物理内存的转换.假如在32位环境上,理论上我们可以使用的虚拟内存空间是4GB,但实际上这4GB并不能完全给 ...
- Storm 开箱笔记
目录 Storm 开箱 1. 什么是 Storm 2. Hello World(WordCountTopology) 3. 常用API 4. 基本概念 5. 流分组策略 6. 并行度 7. Acker ...
- Linux:RHEL7图形界面转文字
1.7 Systemd初始化进程 Linux操作系统开机过程首先从BIOS开始→进入"Boot Loader"→加载系统内核→内核进行初始化→启动初始化进程.初始化进程作为系统第一 ...
- 马拉车,O(n)求回文串
马拉车,O(n)求回文串 对整个马拉车算法步骤做个总结: 第一步:将每个原字母用两个特殊字符包围如: aaa --> #a#a#a# abab -->#a#b#a#b 同时可以由这个翻倍的 ...
- ds020507
芯片输出端不加负载的时候,芯片的输出电压是9点多伏. 加上大的负载,芯片发热,电压接近输入电压. 正常负载,芯片输出7.0几伏. 版权声明:本文为博主原创文章,未经博主允许不得转载.