码迷,mamicode.com
首页 > 其他好文 > 详细

Generalized Abbreviation

时间:2016-07-05 06:27:00      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

 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. 

Generalized Abbreviation

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/5642228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!