【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”, “3d”, “w3”, “4”]

思路:递归+回溯

class Solution(object):
    def generateAbbreviations(self, word):
        """ :type word: str :rtype: List[str] """

        res = []
        #从0开始遍历,最后0用于计数
        self.helper( 0, word, "", res, 0)
        return res

    def helper(self, index, word, temp_res, res, count):
        #注意这里是len(word),不是len(word)-1
        if index==len(word):
            if count!=0:
                #组合新的字符串
                temp_res = temp_res[0:-count] + str(count)
            res.append( temp_res )
            return

        # append 1
        self.helper( index+1, word, temp_res+‘1‘, res, count+1)

        # append word
        if count!=0:
            temp_res = temp_res[0:-count] + str(count)
        self.helper( index+1, word, temp_res+word[index], res, 0)
s=Solution()
print(s.generateAbbreviations("word"))

只能看代码慢慢理解了。

参考:https://www.it610.com/article/5456538.htm

原文地址:https://www.cnblogs.com/xiximayou/p/12557176.html

时间: 2024-11-07 19:16:37

【python-子集】Generalized Abbreviation(广义缩写)的相关文章

[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] 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

[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

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

[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

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

[LeetCode] Valid Word Abbreviation 验证单词缩写

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &