249. 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:
|
|
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:123456[ ["abc","bcd","xyz"], ["az","ba"], ["acef"], ["a","z"]]
很简单的一个题目。
- 要想把同一类型的字符串归类,要选择一种变形方式,让所有同类型的字符串能变形成为同一个字符串
- 然后以这个字符串做为HashMap的Key,来归类字符串
- 我选择的方式,字符串中所有的字符,减去第一个字符。所有字符向0看齐
- 有一点需要注意的是,”ba” 这种字符串,后面的字符有的比第一个小,减完之后是负数,这时候就要加上一个26.
代码如下:
|
|