Generalized Abbreviations

Lesson Learned: Recursion

Write a function to generate the generalized abbreviations of a word (order does not matter).

Example: “word” -> “1ord”, “w1rd“, “wo1d”, “wor1”, “2rd”, “2r1”, “w2d”, “wo2”, “1o2”, “3d”, “w3”, “4”

The approach here is to choose whether to abbreviate each character or not. If we do abbreviate the character then we need to increment a counter for the number of characters we have abbreviated so far. If we do not abbreviate the character, we append the character to our “current” string, along with a counter if there is one. Performing this continuously results in a recursive solution which is defined below.