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"]
]
public class Solution {
public List<List<String>> groupStrings(String[] strings) {
List<List<String>> resLists = new LinkedList<List<String>>();
HashMap<String,List<String>> patternMap = new HashMap<String,List<String>>();
for (String str : strings){
StringBuilder builder = new StringBuilder().append(str);
int delta = builder.charAt(0)-'a';
builder.setCharAt(0,'a');
for (int i=1;i<builder.length();i++){
char c = (char) ((builder.charAt(i) + 26 - delta)%26);
builder.setCharAt(i,c);
}
String pattern = builder.toString();
if (!patternMap.containsKey(pattern)){
patternMap.put(pattern,new LinkedList<String>());
}
patternMap.get(pattern).add(str);
}
for (List<String> strList : patternMap.values()){
resLists.add(strList);
}
return resLists;
}
}
LeetCode-Group Shifted Strings的更多相关文章
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- 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: & ...
随机推荐
- Objective-C中常用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect
本节要点:红色标记 需要记下来 1 NSRange typedef struct _NSRange { NSUInteger location; NSUInteger length ...
- 8 Regular Expressions You Should Know
Regular expressions are a language of their own. When you learn a new programming language, they're ...
- C语言指针学习(续)
五.数组和指针的关系 int array[10] = {0,1,2,3,4,5,6,7,8,9},value; ... ... value = array[0];//也可以写成 value = *ar ...
- python 小程序练习
还有一些小bug 基本有 输入用户名密码 认证成功后显示欢迎信息 输出三次后锁定 # -*- coding:utf-8 -*- account_file=('C:\Users\guigu\Deskto ...
- char,vchar,nchar,nvchar的区别
char和varchar的长度都在1到8000之间,它们的区别在于char是定长字符数据,而varchar是变长字符数据.所谓定长就是长度固定的,当输入的数据长度没有达到指定的长度时将自动以英文空格在 ...
- uva 122 trees on the level——yhx
题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...
- IOS Orientation, 想怎么转就怎么转~~~
此博文主要针对IOS应用, 是屏幕旋转相关问题的一个总结. 主要内容有: IOS5,6,7不同版的适配. 强制旋转和自动旋转. 博客: http://www.cnblogs.com/jhzhu 邮箱: ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- UVA-10652 (凸包)
题意: 给n个矩形,问包含这些矩形的尽量小的凸多边形的面积是多少; 思路: 由于给的矩形的形式是给出了中心的坐标,长和宽以及旋转的角度,所以先转换成四个点的坐标,然后求一遍凸包就好了,第一次写凸包,代 ...
- UVALive 6470 Chomp --记忆化搜索
题意:给一个只有三行的方块阵(横向最多100个),然后p,q,r分别代表第1,2,3层的方格数,两人轮流去掉一个格子,此时这个格子的右上方都会被去掉,面临只剩最左下角的一个格子的状态的人输,问先手能否 ...