File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ package easy ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Collections ;
5+ import java .util .HashMap ;
6+ import java .util .List ;
7+ import java .util .Map ;
8+
9+ public class GroupShiftedStrings {
10+
11+ public List <List <String >> groupStrings (String [] strings ) {
12+
13+ List <List <String >> result = new ArrayList <List <String >>();
14+ Map <String , List <String >> map = new HashMap <String , List <String >>();
15+
16+ for (String word : strings ){
17+ String key = "" ;
18+ int offset = word .charAt (0 ) - 'a' ;
19+ for (int i = 1 ; i < word .length (); i ++){
20+ key += (word .charAt (i ) - offset + 26 )%26 ;
21+ }
22+
23+ if (!map .containsKey (key )) map .put (key , new ArrayList <String >());
24+ map .get (key ).add (word );
25+ }
26+
27+ for (List <String > list : map .values ()){
28+ Collections .sort (list );
29+ result .add (list );
30+ }
31+
32+ return result ;
33+
34+ }
35+
36+ }
Original file line number Diff line number Diff line change 6060| 259| [ 3Sum Smaller] ( https://leetcode.com/problems/3sum-smaller/ ) | [ Solution] ( ../../blob/master/MEDIUM/src/medium/_3Sum_Smaller.java ) | O(n^2)| O(1) | Medium|
6161|257|[ Binary Tree Paths] ( https://leetcode.com/problems/binary-tree-paths/ ) |[ Solution] ( ../../blob/master/EASY/src/easy/BinaryTreePaths.java ) | O(n* h) | O(h) | DFS/Recursion
6262| 252| [ Meeting Rooms] ( https://leetcode.com/problems/meeting-rooms/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/MeetingRooms.java ) | O(nlogn) | O(1) |
63+ | 249| [ Group Shifted Strings] ( https://leetcode.com/problems/group-shifted-strings/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/GroupShiftedStrings.java ) | O(nlogn) | O(n) |
6364| 246| [ Strobogrammatic Number] ( https://leetcode.com/problems/strobogrammatic-number/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/StrobogrammaticNumber.java ) | O(n) | O(1) |
6465| 243| [ Shortest Word Distance] ( https://leetcode.com/problems/shortest-word-distance/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/ShortestWordDistance.java ) | O(n) | O(1) |
6566| 223| [ Rectangle Area] ( https://leetcode.com/problems/rectangle-area/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/RectangleArea.java ) | O(1)| O(1) | Easy|
You can’t perform that action at this time.
0 commit comments