Generalized Abbreviation
来源:互联网 发布:d h算法 编辑:程序博客网 时间:2024/04/30 22:40
Write a function to generate the generalized abbreviations of a word.
Example:
Given word = "word"
, return the following list (order does not matter):
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]思路:backtracking,从0开始替换,从1开始替换,注意两点。
1. list必须把原来的string加进去。
2. 再进行下一次循环的时候,是基于已经变换好的string,然后往下传,然后start index应该是新生成的的string的i+num.length()+1,因为要隔一个才能生成新的number。
public class Solution { public List<String> generateAbbreviations(String word) { List<String> list = new ArrayList<String>(); if(word == null) return list; list.add(word); collect(word, 0, list); return list; } public void collect(String s, int start, List<String> list){ if(start >= s.length()){ return; } for(int i=start; i<s.length(); i++){ for(int j=1; i+j<=s.length(); j++){ String num = String.valueOf(j); String abbv = s.substring(0,i) + num +s.substring(i+j); list.add(abbv); collect(abbv, i+1+num.length(), list); } } }}
0 0
- Generalized Abbreviation
- Generalized Abbreviation
- Leetcode: Generalized Abbreviation
- [leetcode320]Generalized Abbreviation
- 320. Generalized Abbreviation
- [LeetCode]Generalized Abbreviation
- leetcode Generalized Abbreviation
- LeetCode 320. Generalized Abbreviation
- 320. Generalized Abbreviation
- Leetcode 320. Generalized Abbreviation
- [leetcode] 320. Generalized Abbreviation 解题报告
- LeetCode 320. Generalized Abbreviation(产生缩写)
- [Leetcode] 320. Generalized Abbreviation 解题报告
- Abbreviation
- abbreviation
- Abbreviation
- Abbreviation
- abbreviation
- EventBus使用
- [笔记] Golang小试实现神经网络框架
- 关于maven可选依赖和排除依赖的学习
- 当游戏爱上MongoDB会怎么样???
- 掌握 Linux PC 性能之基准测试
- Generalized Abbreviation
- SQL 之 触发器
- ubuntu下的两种应急方法
- codevs 1557 热浪
- Leetcode 81. Search in Rotated Sorted Array II (Medium) (java)
- LDAP简介
- JAVA中的接口和抽象类(转)
- CSS的2个冷门却又实用的选择器
- 2016年学哪个编程语言最有前途?