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

320. Generalized Abbreviation

时间:2016-09-17 07:13:08      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/renyualbert/p/5877852.html

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