Easy to understand 16ms JAVA solution with backtracking


  • 0
    G
       public List<String> generateAbbreviations(String word) {
            List<String> list = new LinkedList<>();
            backtrack(word, 0, list);
            return list;
        }
        
        public void backtrack(String word, int start, List<String> list) {
            list.add(word);
            if (start >= word.length()) return;
            for (int i = 1; i <= word.length() - start; ++i) {
                for (int j = start; j <= word.length() - i; ++j) {
                    backtrack(word.substring(0, j) + i + word.substring(j + i, word.length()), j + 2 + i / 10, list);
                }
            }
            return;
        }

Log in to reply
 

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