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", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
 1 public class Solution {
 2     public IList<string> GenerateAbbreviations(string word) {
 3         var result = new List<string>() {};
 4
 5         DFS(word, 0, "", false, result);
 6
 7         return result;
 8     }
 9
10     private void DFS(string word, int start, string cur, bool lastDigit, IList<string> result)
11     {
12         if (start >= word.Length)
13         {
14             result.Add(cur);
15
16             return;
17         }
18
19         DFS(word, start + 1, cur + word[start], false, result);
20
21         if (!lastDigit)
22         {
23             for (int j = 1; start + j <= word.Length; j++)
24             {
25                 DFS(word, start + j, cur + j.ToString(), true, result);
26             }
27         }
28     }
29 }

原文地址:https://www.cnblogs.com/liangmou/p/8133159.html

时间: 2024-12-17 08:00:47

Leetcode 320: Generalized Abbreviation的相关文章

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

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

LeetCode Valid Word Abbreviation

原题链接在这里:https://leetcode.com/problems/valid-word-abbreviation/#/description 题目: 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 followi

[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: Unique Word Abbreviation

An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations: a) it --> it (no abbreviation) 1 b) d|o|g --> d1g 1 1 1 1---5----0----5--8 c) i|nternationalizatio|n --&

320. Generalized Abbreviation-- back tracking

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