博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode – Group Shifted Strings
阅读量:7254 次
发布时间:2019-06-29

本文共 1499 字,大约阅读时间需要 4 分钟。

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"]]

与类似. 维护一个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 = groupStrings(strs); for(int i=0; i
subList = list.get(i); for(int j=0; j
> groupStrings(String[] strings) { if(strings == null || strings.length == 0){ return new ArrayList
>(); } Map
> map = new HashMap<>(); for(int i=0; i
()); } map.get(str).add(strings[i]); } return new ArrayList
>(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

  

  

转载于:https://www.cnblogs.com/incrediblechangshuo/p/9352106.html

你可能感兴趣的文章
ubuntu搭建mediawiki
查看>>
uoj#274. 【清华集训2016】温暖会指引我们前行(LCT)
查看>>
[51nod1222] 最小公倍数计数(莫比乌斯反演)
查看>>
LeetCode - 37. Sudoku Solver
查看>>
公钥,私钥和数字签名这样最好理解
查看>>
SqlBulkCopy 类
查看>>
csu1811(树上启发式合并)
查看>>
spring 整合maven 定时任务调度,多个任务写法
查看>>
New Concept English Two 15 37
查看>>
L125
查看>>
poj2192
查看>>
json的内容回顾
查看>>
SAP将内表生成XML作为excel保存到FTP
查看>>
栅格系统
查看>>
[Selenium] 使用自定义的FirefoxProfile
查看>>
Spine批量导出Command line Export
查看>>
POJ3690:Constellations——题解
查看>>
第二次毕业设计任务书
查看>>
Maven中POM.XML详解
查看>>
小时候,长大了
查看>>