Leetcode 320: Generalized Abbreviation

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 



以上是关于Leetcode 320: Generalized Abbreviation的主要内容,如果未能解决你的问题,请参考以下文章

320. Generalized Abbreviation

320. Generalized Abbreviation

320. Generalized Abbreviation

LeetCode Generalized Abbreviation

[LeetCode] Generalized Abbreviation 通用简写

LeetCode笔记:Weekly Contest 320