Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1119b9f

Browse files
group shifted strings
1 parentd8a5e91 commit1119b9f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packageeasy;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Collections;
5+
importjava.util.HashMap;
6+
importjava.util.List;
7+
importjava.util.Map;
8+
9+
publicclassGroupShiftedStrings {
10+
11+
publicList<List<String>>groupStrings(String[]strings) {
12+
13+
List<List<String>>result =newArrayList<List<String>>();
14+
Map<String,List<String>>map =newHashMap<String,List<String>>();
15+
16+
for(Stringword :strings){
17+
Stringkey ="";
18+
intoffset =word.charAt(0) -'a';
19+
for(inti =1;i <word.length();i++){
20+
key += (word.charAt(i) -offset +26)%26;
21+
}
22+
23+
if(!map.containsKey(key))map.put(key,newArrayList<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+
returnresult;
33+
34+
}
35+
36+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
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|

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp