Need help on my Java solution.


  • 0
    X

    My solution failed the test at 'interaction', but I can't figure out which was missing since the output is so long, and I ran the code through shorter words and it seems to be working fine, below is my code:

    public class Solution {
    Set<String> result = new HashSet<> ();
    public List<String> generateAbbreviations(String word) {
        if(word == null) return (new ArrayList<String> (result));
        if(word.length() == 0) {
            result.add("");
            return (new ArrayList<String> (result));
        }
        addResult(word, 0);
        return new ArrayList<String> (result);
    }
    
    private void addResult(String word, int position) {
        if(position >= word.length()) {
            result.add(word);
            return;
        }
        addResult(word, position+1);
        for(int i=1; i<=(word.length()-position); i++) {
            addResult(word.substring(0, position)+Integer.toString(i)+word.substring(position+i), position+2);
        }
    }
    

    }


Log in to reply
 

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