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"]
]
 class Solution {
public:
string help(string word) {
for (int i = , n = word.size(); i < n; i++) {
word[i] = word[i] - word[] + 'a';
while (word[i] < 'a') word[i] += ;
}
word[] = 'a';
return word;
}
vector<vector<string>> groupStrings(vector<string>& strings) {
unordered_map<string, int> dict;
vector<vector<string> > res;
for (int i = , n = strings.size(); i < n; i++) {
string base = help(strings[i]);
if (dict.count(base) == ) {
dict.insert(make_pair(base, res.size()));
vector<string> subGroup(, strings[i]);
res.push_back(subGroup);
}
else res[dict[base]].push_back(strings[i]);
}
return res;
}
};

Group Shifted Strings -- LeetCode的更多相关文章

  1. [Locked] Group Shifted Strings

    Group Shifted Strings Given a string, we can "shift" each of its letter to its successive ...

  2. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  3. [LeetCode#249] Group Shifted Strings

    Problem: Given a string, we can "shift" each of its letter to its successive letter, for e ...

  4. LeetCode 249. Group Shifted Strings (群组移位字符串)$

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  5. [LeetCode] 249. Group Shifted Strings 分组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  6. LeetCode – Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. 249. Group Shifted Strings

    题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...

  8. [Swift]LeetCode249.群组偏移字符串 $ Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  9. Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

随机推荐

  1. c# asp.net 中使用token验证

    基于token的鉴权机制类似于http协议也是无状态的,它不需要在服务端去保留用户的认证信息或者会话信息.这就意味着基于token认证机制的应用不需要去考虑用户在哪一台服务器登录了,这就为应用的扩展提 ...

  2. Oz 创建Debian8镜像

    <template> <name>Debian8.7-zxy</name> <os> <name>Debian</name> & ...

  3. 简单的JS钟表计时

    思路:先写出简单的数字计时,根据时分秒的数值转换成度数,使用CSS3的transform进行div倾斜. 知识点:transform可以对div进行倾斜或旋转等效果.但是根据浏览器不同代码也不同,本代 ...

  4. 使用hadoop统计多个文本中每个单词数目

    程序源码 import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Con ...

  5. JS设置、获取DOM自定义属性

    jQuery方式 // 获取 $('#test').attr('mydata'); // 设置 $('#test').attr('mydata','data-content'); // 移除 $('# ...

  6. 雅礼集训 Day3 T2 u 解题报告

    u 题目背景 \(\frac 14\) 遇到了一道水题,完全不会做,于是去请教小\(\text{D}\).小\(\text{D}\)看了一眼就切掉了这题,嘲讽了\(\frac 14\)一番就离开了. ...

  7. 2-sat 学习笔记

    一.问题描述 以你咕的模板题为例 题目描述 有\(n\)个布尔变量\(x_1\)~\(x_n\),另有\(m\)个需要满足的条件,每个条件的形式都是"\(x_i\)为true/false或\ ...

  8. 《机器学习实战》读书笔记—k近邻算法c语言实现(win下)

    #include <stdio.h> #include <io.h> #include <math.h> #include <stdlib.h> #de ...

  9. forEach循环dom元素

    //让ie8支持foreach if (typeof Array.prototype.forEach != 'function') { Array.prototype.forEach = functi ...

  10. js 数组遍历

    map.filter.forEach.every.some http://www.runoob.com/jsref/jsref-obj-array.html 1.在字符串中使用 map 在一个Stri ...