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