LeetCode – Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence: "abc" -> "bcd" -> ... -> "xyz"
Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence. For example, given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"],
A solution is: [
["abc","bcd","xyz"],
["az","ba"],
["acef"],
["a","z"]
]
与Group Anagrams类似. 维护一个HashMap, key是每个string 的 base型.
Time Complexity: O(n * strings.length), n 是每个string的平均长度.
Space: O(hm.size()), HashMap size.
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
String[] strs = {"abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"};
List<List<String>> list = groupStrings(strs);
for(int i=0; i<list.size(); i++){
System.out.println("List "+(i+1));
List<String> subList = list.get(i);
for(int j=0; j<subList.size(); j++){
System.out.println(subList.get(j));
}
} } public static List<List<String>> groupStrings(String[] strings) {
if(strings == null || strings.length == 0){
return new ArrayList<List<String>>();
} Map<String, List<String>> map = new HashMap<>();
for(int i=0; i<strings.length; i++){
String str = getBase(strings[i]);
if(!map.containsKey(str)){
map.put(str, new ArrayList<String>());
}
map.get(str).add(strings[i]); }
return new ArrayList<List<String>>(map.values()); } public static String getBase(String str){
if(str == null || str.length()==0){
return str;
}
StringBuilder sb = new StringBuilder();
int offset = str.charAt(0) - 'a';
for(int i=0; i<str.length(); i++){
char c = (char) (str.charAt(i)-offset);
if(c < 'a'){
c += 26;
}
sb.append(c); }
return sb.toString();
}
}
LeetCode – Group Shifted Strings的更多相关文章
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [Locked] Group Shifted Strings
Group Shifted Strings Given a string, we can "shift" each of its letter to its successive ...
- [LeetCode#249] Group Shifted Strings
Problem: Given a string, we can "shift" each of its letter to its successive letter, for e ...
- LeetCode 249. Group Shifted Strings (群组移位字符串)$
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] 249. Group Shifted Strings 分组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- Group Shifted Strings -- LeetCode
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- 249. Group Shifted Strings
题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...
- [Swift]LeetCode249.群组偏移字符串 $ Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- Group Shifted Strings
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
随机推荐
- 多线程处理慢sql查询小笔记~
多线程处理慢sql查询以及List(Array)的拆分 系统数据量不大,但是访问速度特别慢,使用多线程优化一下!!! 优化结果:访问时间缩短了十几秒 25s --> 8s 一.List的拆分: ...
- ADO.NET 连接池 Session 状态分析
ADO.NET 中提供连接池避免 在业务操作中频繁打开,关闭连接. 当客户端释放连接后,连接池并未真正将数据库连接资源释放 , 而是根据连接字符串特征,将资源放到连接池中, 方便下次重用. 因此问题来 ...
- SQL-26 (二次分组)汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
题目描述 汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no.dept_name.其当前员工所有的title以及该类型title对应的数目countCREATE TABLE ...
- 20165326 java第九周学习笔记
第九周学习笔记 URL类 属于java.net包 最基本三部分:协议(对象所在的Java虚拟机支持).地址(能连接的有效IP地址或域名).资源(主机上的任何一个文件) 常用构造方法 public UR ...
- TkbmMWFileClient产生的Timeout/error waiting for connection.
当文件客户端TkbmMWFileClient产生这个错误,该怎么解决掉呢? 解决方法: 设置ConnectionWaitTimeout为更长时间,这个时间以毫秒为单位,设置20*1000,20秒.
- java学习笔记5(方法)
方法: 1.如何创建方法 修饰符 返回值类型 方法名(参数){被封装的代码段} 2.方法的定义和使用的注意事项: a:方法不能定义在另一个方法里面: b:方法 名字和方法的参数列表,定义和调用时 ...
- 【Python】Excel-4(样式设置)
#练习: #封装一个ExcelUtil的模块(构造函数是excel的路径),里面提供封装的方法: #1 获取某个sheet对象 #2 打印所有sheet名称 #3 给某个sheet的某个单元格写入内容 ...
- 基于区域的OSPF的MD5认证
实验要求:掌握OSPF基于区域的MD5认证 拓扑如下: 配置如下: R1enable configure terminal interface s0/0/0ip address 192.168.1.1 ...
- ORACLE函数、连接查询、约束
*ORDER BY 子句在SELECT语句的结尾. 使用外连接可以查询不满足连接条件的数据 with字句 字符函数lower upper initcap concat substr length in ...
- Swig--模板引擎
{% filter uppercase %} oh hi, {{ name }} {% endfilter %} {% filter replace "." "!&quo ...