Skip to content

Commit 0a1368b

Browse files
committed
fd
1 parent a6c1266 commit 0a1368b

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
|245|[Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ShortestWordDistanceIII.java)|80||
229229
|246|[Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/StrobogrammaticNumber.java)|70|#247一个系列,多看两遍|
230230
|247|[Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/StrobogrammaticNumberII.java)|70|多看两遍|
231+
|249|[Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/GroupShiftedStrings.java)||
231232
|250|[Count Univalue Subtrees](https://leetcode.com/problems/count-univalue-subtrees/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/CountUnivalueSubtrees.java)|70|这题要多做几遍|
232233
|251|[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/Vector2D.java)|100|
233234
|252|[Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MeetingRooms.java)|100|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.inuker.solution;
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+
/**
10+
* Created by liwentian on 2017/12/9.
11+
*/
12+
13+
public class GroupShiftedStrings {
14+
/**
15+
* 这里核心是getTag,不论怎么shift,都回滚到a开头
16+
*/
17+
public List<List<String>> groupStrings(String[] strings) {
18+
Map<String, List<String>> map = new HashMap<>();
19+
20+
for (String s : strings) {
21+
String key = getTag(s);
22+
List<String> value = map.get(key);
23+
if (value == null) {
24+
value = new ArrayList<>();
25+
map.put(key, value);
26+
}
27+
28+
value.add(s);
29+
}
30+
31+
List<List<String>> ret = new ArrayList<>();
32+
for (List<String> lst : map.values()) {
33+
Collections.sort(lst); // dont forget to sort.
34+
ret.add(lst);
35+
}
36+
37+
return ret;
38+
}
39+
40+
String getTag(String s) {
41+
int diff = (int) s.charAt(0) - (int) 'a';
42+
43+
StringBuilder sb = new StringBuilder();
44+
for (char c : s.toCharArray())
45+
sb.append((c + 26 - diff) % 26);
46+
47+
return sb.toString();
48+
}
49+
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
public class main {
2929

3030
public static void main(String[] args) {
31+
System.out.println(getTag("abc"));
32+
System.out.println(getTag("bcd"));
33+
System.out.println(getTag("cde"));
3134
}
3235

36+
static String getTag(String s) {
37+
int diff = (int) s.charAt(0) - (int) 'a';
38+
39+
StringBuilder sb = new StringBuilder();
40+
for (char c : s.toCharArray())
41+
sb.append((c + 26 - diff) % 26);
42+
43+
return sb.toString();
44+
}
3345
}

0 commit comments

Comments
 (0)