320. Generalized Abbreviation-- back tracking

把一个字符串中字母用数字代替,产生所有的组合数Input: "word"
Output:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

和78 subsets本质上也是一样的: https://leetcode.com/problems/subsets/description/

画出递归数吧: 假设word = "abc", 递归数如下:

需要注意的是, 如果某个节点已经是数字了,则接下来不能继续放数字 ,例如 "a11" 是不合法的。

先写了如下code ,在 input = "interaction" 时WA了, 而其他短字符串都可以过。

因为 最后一行 “curResult.deleteCharAt(curResult.length()-1);”   input 字符串长度超过10后,每次加的长度 就是2位数了, 所以不能 只 减去1。

class Solution {
    public List<String> generateAbbreviations(String word) {
        List<String> result = new ArrayList<>();

        dfs(new StringBuilder(), result, word,0);
        return result;
    }

    private void dfs(StringBuilder curResult,List<String> result, String word, int cur_index){
        if(curResult.length() == word.length() || cur_index == word.length()){
            result.add(curResult.toString());
            return;
        }

        // put cur letter
        curResult.append(word.charAt(cur_index));
        dfs(curResult,result, word,cur_index+1);
        curResult.deleteCharAt(curResult.length()-1);

        for(int i=1; i<=word.length()-cur_index; i++){ // wrd: cur_index = 0, len =3, 可以放 1,2,3
            // 当前result 里 最后一个字符得是字母才能 放数字
            if(curResult.length()==0 || Character.isLetter(curResult.charAt(curResult.length()-1)) ){
                curResult.append(i);
                dfs(curResult,result,word,cur_index+i);
                curResult.deleteCharAt(curResult.length()-1);
            }

        }
    }

}

修改后的code, 每次记录 上一次的stringbuild 长度,然后  重新setLeng 到之前的长度即可:

class Solution {
    public List<String> generateAbbreviations(String word) {
        List<String> result = new ArrayList<>();
        dfs(new StringBuilder(), result, word,0);
        return result;
    }

    private void dfs(StringBuilder curResult,List<String> result, String word, int cur_index){
        if(curResult.length() == word.length() || cur_index == word.length()){
            result.add(curResult.toString());
            return;
        }

        int len = curResult.length(); // 每次记录长度
        // put cur letter
        curResult.append(word.charAt(cur_index));
        dfs(curResult,result, word,cur_index+1);
        curResult.setLength(len);
        //put the 剩下的可能的长度
        for(int i=1; i<=word.length()-cur_index; i++){ // wrd: cur_index = 0, len =3, 可以放 1,2,3
            // 当前result 里 最后一个字符得是字母才能 放数字
            if(curResult.length()==0 || Character.isLetter(curResult.charAt(curResult.length()-1)) ){
                curResult.append(i);
                dfs(curResult,result,word,cur_index+i);
                curResult.setLength(len); //恢复之前的长度
            }
        }
    }
}


原文地址:https://www.cnblogs.com/keepAC/p/9944641.html

时间: 2024-11-08 22:52:37

320. Generalized Abbreviation-- back tracking的相关文章

Leetcode 320: Generalized Abbreviation

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", &qu

320. Generalized Abbreviation

这道题没有做出来不应该. 显然是backtracking. 分两种情况: 一种是到当前位置的字母重新开始计数 一种是继续前一个字母的计数 需要注意的是,如果cnt==0是不要加到结果String里的 1 public List<String> generateAbbreviations(String word) { 2 List<String> res = new ArrayList<String>(); 3 if(word == null) { 4 return re

[Locked] Generalized Abbreviation

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", &quo

[LeetCode] Generalized Abbreviation 通用简写

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", &qu

【python-子集】Generalized Abbreviation(广义缩写)

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”,

[LeetCode]Generalized Abbreviation

感觉我的方法逻辑不够清晰 public class Solution { private List<String> result; public List<String> generateAbbreviations(String word) { result = new ArrayList<String>(); helper(word, -2, 0); result.add(word); return result; } public void helper(Strin

Generalized Abbreviation

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 i

[LeetCode] 527. Word Abbreviation 单词缩写

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then the number of characters abbreviated, which followed by the last charact

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre