首页 > 代码库 > 320. Generalized Abbreviation
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"]
public IList<string> GenerateAbbreviations(string word) { List<string> res = new List<string>(); BackTracking(word,0,"",res,0); return res; } private void BackTracking(string word,int sentinel, string cur, List<string> res, int isNumberbefore) { if(sentinel == word.Length) { if(isNumberbefore ==0) res.Add(cur); else res.Add(cur+isNumberbefore); } else { if(isNumberbefore > 0) BackTracking(word, sentinel+1,cur+isNumberbefore+word[sentinel],res,0); else BackTracking(word, sentinel+1,cur+word[sentinel],res,0); BackTracking(word, sentinel+1,cur,res,isNumberbefore+1); } }
320. Generalized Abbreviation
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。