return The following list (order does not matter): ["word", "1ord", "W1rd", "wo1d", "Wor1", "2rd", "w2d", "WO2", "1o1d", "1or1", "W1r1", "1o2", "2r1", "3d", "W3", "4"]
This problem must be dfs/backtracking, but how Dfs bad think, and leetcode:remove Invalid parentheses backtracking very much like.
Generalized abbreviation This question is the current letter to do not abbreviate, to or not two options, parentheses that the question is the current bracket should not keep in stringbuffer, to or not the same two options.
Syntax: Note that 27 lines use Stringbuffer.setlength (), because count always accumulates may become two-digit three-digit number, delete StringBuffer The last letter may not, So simply set the length of the initial into recursion
Referenced by: https://leetcode.com/discuss/76783/easiest-14ms-java-solution-beats-100%25
1 Public classSolution {2 PublicList<string>generateabbreviations (String word) {3list<string> res =NewArraylist<string>();4DFS (0, Word.tochararray (),NewStringBuffer (), 0, res);5 returnRes;6 }7 8 Public voidDfsintPosChar[] Word, StringBuffer SB,intCount, list<string>Res) {9 intLen =word.length;Ten intSboriginsize =sb.length (); One if(pos = =Len) { A if(Count > 0) { - Sb.append (count); - } the Res.add (sb.tostring ()); - } - Else { - //choose to abbr Word[pos] +DFS (pos+1, Word, SB, count+1, res); - + //choose not to abbr Word[pos] A //First Append previous count to SB if Count>0 at if(Count > 0) Sb.append (count); - sb.append (Word[pos]); -DFS (pos+1, Word, SB, 0, res); - } - sb.setlength (sboriginsize); - } in}
leetcode:generalized abbreviation