forked from jsjtzyy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC320_GeneralizedAbbreviation.java
More file actions
45 lines (45 loc) · 1.27 KB
/
LC320_GeneralizedAbbreviation.java
File metadata and controls
45 lines (45 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
/*
BFS
*/
public class LC320_GeneralizedAbbreviation {
public List<String> generateAbbreviations(String word) {
List<String> res = new ArrayList<>();
if(word == null || word.length() == 0) {
res.add(new String());
return res;
}
Queue<String> cur = new LinkedList<>();
int index = 0, len = word.length();
cur.offer(word.substring(index, index + 1));
cur.offer("1");
String tmp = null;
int cnt = 0;
char ch = ' ';
Queue<String> next = null;
++index;
while(index < len){
next = new LinkedList<>();
while(!cur.isEmpty()){
tmp = cur.poll();
cnt = tmp.length() - 1;
while(cnt >= 0){
ch = tmp.charAt(cnt);
if(Character.isLetter(ch)) break;
cnt--;
}
if(cnt == tmp.length() - 1)
next.offer(tmp + "1");
else
next.offer(tmp.substring(0, cnt + 1) + (Integer.parseInt(tmp.substring(cnt + 1, tmp.length())) + 1));
next.offer(tmp + word.substring(index, index + 1));
}
cur = next;
++index;
}
while(!cur.isEmpty()){
res.add(cur.poll());
}
return res;
}
}