1 public class Solution {
 2     public List<String> generateAbbreviations(String word) {
 3         List<String> result = new ArrayList<>();
 4 
 5         getAbb(word, 0, 0, "", result);
 6         return result;
 7     }
 8     
 9     private void getAbb(String word, int index, int count, String str, List<String> result) {
10         if (index == word.length()) {
11             if (count > 0) {
12                 str += count;
13             }
14             result.add(str);
15             return;
16         }
17         
18         getAbb(word, index + 1, count + 1, str, result);
19         getAbb(word, index + 1, 0, str + (count > 0 ? count : "") + word.charAt(index), result);
20     }
21 }

1. Do not need to check boundary case since the helper function can add "" into result.

2. Do not forget to add that character if not counting into abb. 

posted on 2016-07-05 06:12  keepshuatishuati  阅读(147)  评论(0编辑  收藏  举报