首页 > 其他 > 详细

Generalized Abbreviation

时间:2016-07-05 06:25:03      阅读:136      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!