We can observe that the rule is to replace substring with length but two consecutive replacement are illegal.
class Solution(object): def generateAbbreviations(self, word): res =  self.dfs(word, 0, res) return res def dfs(self, word, index, r): r.append(word) for s in xrange(1, len(word) + 1): for i in xrange(index, len(word)): if len(word) - i >= s: self.dfs(word[:i]+str(s)+word[i+s:], i+len(str(s))+1, r)
You don't think it's longer than my first one?
@jedihy What's the runtime for this? Did you try to analyze it?
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.