Clean Java Recursive Solution

  • 0

    The tricky part is to remember remove the trailing characters (for previous count) of accumulated word.

    public class Solution {
        private void helper(String word, int preCount, String currW, List<String> res) {
            else {
                helper(word.substring(1), 0, currW+word.charAt(0), res);
                int end = preCount==0 ? currW.length() : (currW.length()-(""+preCount).length());
                helper(word.substring(1), preCount+1, currW.substring(0,end)+(preCount+1), res);
        public List<String> generateAbbreviations(String word) {
            List<String> res = new ArrayList<String>();
            helper(word, 0, "", res);
            return res;

Log in to reply

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