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中偶数位出现的字 ...
随机推荐
- Xilinx FPGA编程技巧之常用时序约束详解
1. 基本的约束方法 为了保证成功的设计,所有路径的时序要求必须能够让执行工具获取.最普遍的三种路径为: 输入路径(Input Path),使用输入约束 寄存器到寄存器路径(Register-to ...
- ubuntu部署java环境
一.安装java sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracl ...
- 【sqli-labs】 less64 GET -Challenge -Blind -130 queries allowed -Variation3 (GET型 挑战 盲注 只允许130次查询 变化3)
双括号整型 http://192.168.136.128/sqli-labs-master/Less-64/?id=1)) or ((1
- CAD利用Select2得到所有实体(网页版)
主要用到函数说明: IMxDrawSelectionSet::Select2 构造选择集.详细说明如下: 参数 说明 [in] MCAD_McSelect Mode 构造选择集方式 [in] VARI ...
- ThinkPHP---thinkphp模型(M)拓展
(1)创建数据对象 数据对象就是父类模型中的$this->data,AR模式的底层数据操作用到了数据对象.模型实例化之前数据对象只是空数组,后来使用了魔术方法__set设置了数据对象的值. 上述 ...
- 04C语言输入输出
C语言输入输出 输入字符getchar() #include <stdio.h> int main(){ putchar(getchar()); putchar(getchar()); ; ...
- Python自学-1-基本概念问题
C语言适合开发那些追求运行速度.充分发挥硬件性能的程序. Python是用来编写应用程序的高级编程语言. Python提供了 第三方库 & 基础代码库(覆盖了网络.文件.GUI.数据库.文本等 ...
- cgroup代码浅析(1)
前置:这里使用的linux版本是4.8,x86体系. cgroup_init_early(); 聊这个函数就需要先了解cgroup. cgroup概念 这个函数就是初始化cgroup所需要的参数的.c ...
- JAVA基础——文件File简单实用
1.1java.io.File File用于表示文件系统中的一个文件或目录 通过File可以: 1:访问该文件或目录的属性信息(名字,大小,修改时间等) file.getName();获取文件名fil ...
- Java基础——二分法
BinarySearch 二分法查找,顾名思义就是要将数据每次都分成两份然后再去找到你想要的数据,我们可以这样去想,二分法查找很类似与我们平时玩的猜价格游戏,当你报出一个价格时裁判会告诉你价格相对于真 ...