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中偶数位出现的字 ...
随机推荐
- Appium Python API 汇总(中文版)
网络搜集而来,留着备用,方便自己也方便他人.感谢总结的人! 1.contexts contexts(self): Returns the contexts within the current ses ...
- Flask框架 之第一个Flask程序
from flask import Flask # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录为总目录,static目录为静态目录,templates为模 ...
- js异步请求
目前async / await特性并没有被添加到ES2016标准中,但不代表这些特性将来不会被加入到Javascript中.在我写这篇文章时,它已经到达第三版草案,并且正迅速的发展中.这些特性已经被I ...
- JAVA基础——IO流字节流
在Java中把不同的输入输出源(键盘.文件.网路连接)抽象表述为“流”. 1.输入流.输出流 .字节输入流通过FileInputStream和来操作 字节输出流通过FileOutputStream来操 ...
- LeetCode_18 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- 解决移动端 手机号input 属性为 number,maxlength无效情况
<input type="number" oninput="if(value.length>11)value=value.slice(0,11)" ...
- P1060 开心的金明(洛谷,动态规划递推,01背包轻微变形题)
题目链接:P1060 开心的金明 基本思路: 基本上和01背包原题一样,不同点在于这里要的是最大重要度*价格总和,我们之前原题是 f[j]=max(f[j],f[j-v[i]]+p[i]); 那么这里 ...
- 关于dijkstra的小根堆优化
YY引言 在NOI2018D1T1中出现了一些很震惊的情况,D1T1可以用最短路解决,但是大部分人都在用熟知的SPFA求解最短路.而SPFA的最坏复杂度能够被卡到$O(VE)$.就是边的数量乘以点的数 ...
- Placing Lampposts
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=91212#problem/E #include <iostream> #inc ...
- 【Codeforces 1009C】Annoying Present
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实就是让你最后这n个数字的和最大. 加上的x没有关系.因为肯定都是加上n个x 所以直接加上就可以了 主要在于如何选取j 显然我们要找到一个位 ...