码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 320: Generalized Abbreviation

时间:2017-12-28 12:04:16      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:gpo   new   private   blog   body   list   cti   div   oid   

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

标签:gpo   new   private   blog   body   list   cti   div   oid   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!