320. Generalized Abbreviation 单词缩写

Write a function to generate the generalized abbreviations of a word. 

Note: The order of the output does not matter.

Example:

Input: "word"
Output:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

思路:不知道怎么判断字符串长度:加count, 加pose。好吧,dfs里其实什么都能加。

两种DFS都需要走,一种计数,一种打印压缩后的长度

dfs(word, pos + 1, cur, count + 1, results);
dfs(word, pos + 1, cur + (count > 0 ? count : "") + word.charAt(pos), 0, results);
View Code

 

 
posted @ 2020-07-27 11:17  苗妙苗  阅读(94)  评论(0编辑  收藏  举报