請完成一個能夠給出 wordword 的所有“縮寫”的函數(給出任意一種排列即可)
樣例
例1:
輸入:
word = "word",
輸出:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
例2:
輸入:
word = "today"
輸出:
["1o1a1","1o1ay","1o2y","1o3","1od1y","1od2","1oda1","1oday","2d1y","2d2","2da1","2day","3a1","3ay","4y","5","t1d1y","t1d2","t1da1","t1day","t2a1","t2ay","t3y","t4","to1a1","to1ay","to2y","to3","tod1y","tod2","toda1","today"]
class Solution {
public:
/**
* @param word: the given word
* @return: the generalized abbreviations of a word
*/
vector<string> generateAbbreviations(string &word) {
// Write your code here
vector<string>res;
string tmp="";
dfs(word,res,0,tmp,0);
return res;
}
void dfs(string &word,vector<string>&res,int index,string tmp,int cnt)
{
if(index==word.size()){
if(cnt>0) tmp+=to_string(cnt);
res.push_back(tmp);
return;
}
dfs(word,res,index+1,tmp,cnt+1);
if(cnt>0) tmp+=to_string(cnt)+word[index];
else tmp+=word[index];
dfs(word,res,index+1,tmp,0);
}
};