Java accepted DP solution


  • 1
    S

    DP solution.

    public class Solution {
        public List<String> generateAbbreviations(String word) {
            List<String> res = new ArrayList<String>();
            res.add(word);
            
            if (word.length() == 0) return res;
    
            for (int i = 1; i <= word.length(); i++)    {
                List<String> appends = generateAbbreviations(word.substring(i));
                
                for (String append : appends)   {
                    if (append.length() == 0 || (append.charAt(0) < '0' || append.charAt(0) > '9') )
                        res.add("" + i + append);
                    else if (append.charAt(0) >= '0' && append.charAt(0) <= '9') 
                        res.add(word.substring(0, i) + append);
                }
            }
    
            return res;
        }
    }

  • 0
    Y

    It does not work if word contains digits.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.