320.Generalized Abbreviation

https://leetcode.com/problems/generalized-abbreviation/solution/

class Solution {
    public List<String> generateAbbreviations(String word) {
        List<String> ans = new ArrayList<>();
        helper(ans, new StringBuilder(), word, 0, 0);
        return ans;
    }
    public void helper(List<String> ans, StringBuilder sb, String word, int curPos, int curAbbrCnt){
        int originalLen = sb.length();
        if(curPos == word.length()){
            if(curAbbrCnt != 0)
                sb.append(curAbbrCnt);
            ans.add(sb.toString());
        }
        else{
            helper(ans, sb, word, curPos + 1, curAbbrCnt + 1);
            if(curAbbrCnt != 0)
                sb.append(curAbbrCnt);
            sb.append(word.charAt(curPos));
            helper(ans, sb, word, curPos + 1, 0);
        }
        sb.setLength(originalLen);
    }
}

results matching ""

    No results matching ""