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"]
Analysis:
DFS or BFS, replacing letters with numbers according to rules, paying attention to the correctness of recursive conditions and parameters
Code:
voidDfsstring&STR, vector<string> &VR,stringCurintCountinti) {//Condition for ending if(i = =str.length ()) { if(Count >0) cur+=Char(Count +'0'); Vr.push_back (cur); return; } //Stop converting the characters into numbers if(Count >0) DFS (str, VR, cur+Char(Count +'0') + Str[i],0, i +1); ElseDFS (str, VR, cur+ Str[i],0, i +1); //Continue converting ....DFS (str, VR, cur, Count +1, i +1); return;} Vector<string> vs (stringstr) {Vector<string>Vresults; DFS (str, vresults,"",0,0); returnvresults;}
[Locked] Generalized abbreviation