Very concise and short backtracking java solution

  • 1
    public class Solution {
    public List<String> generateAbbreviations(String word) {
        List<String> res = new ArrayList<>();
        backTrack(word, res, "", 0, 0);
        return res;
    public void backTrack(String word, List<String> res, String cur, int pos, int count) {
        if(pos == word.length()) {
            if(count > 0) {
        //not put the character to the current answer
        backTrack(word, res, cur, pos+1, count+1);
        //put the character to the current answer, and clear count
        if(count > 0) {
            backTrack(word, res, cur+count+word.charAt(pos), pos+1, 0);
            backTrack(word, res, cur+word.charAt(pos), pos+1, 0);


Log in to reply

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