Does anyone can help me figure out where is wrong, I had a runtime error.


  • 0
    L
    public class Solution {
        public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
            
            HashMap<Integer, HashSet<String>> eachlevel = new HashMap<Integer, HashSet<String>>();
            ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>(); 
            
            if (start == null || end == null || start.length() == 0 || end.length() == 0 ||
            start.length() != end.length()) {
                return res;
            }
           
            LinkedList<String> queue = new LinkedList<String>();
            int oldnum = 1;
            int curnum = 0;
            int level = 1;
            if (dict.contains(start)) {
                dict.remove(start);
            }
            if (dict.contains(end)) {
                dict.remove(end);
            }
            queue.offer(end);
        
            while (!queue.isEmpty()) {
                String curstr = queue.poll();
                char[] curchar = curstr.toCharArray();
                oldnum--;
                for (int i = 0; i < curstr.length(); i++) {
                    for (char c = 'a'; c <= 'z'; c++ ) {
                        if (c == curchar[i]) {
                            continue;
                        }
                        char old = curchar[i];
                        curchar[i] = c;
                        String newstr = new String(curchar);
                        curchar[i] = old;
                        if (dict.contains(newstr)) {
                            queue.offer(newstr);
                            curnum++;
                            dict.remove(newstr);
                            HashSet<String> set = eachlevel.get(level);
                            if (set != null) {
                                set.add(newstr);
                            }
                            else {
                                HashSet<String> newset = new HashSet<String>();
                                newset.add(newstr);
                                eachlevel.put(level, newset);
                            }
                        }
                    }
                }
                if (oldnum == 0) {
                    oldnum = curnum;
                    curnum = 0;
                    level++;
                }
            }
             HashSet<String> set1 = eachlevel.get(level);
             if (set1 != null) {
                set1.add(start);
             }
             else {
                HashSet<String> newset = new HashSet<String>();
                newset.add(start);
                eachlevel.put(level, newset);
             }
             
             HashSet<String> set2 = eachlevel.get(0);
             if (set2 != null) {
                set2.add(end);
             }
             else {
                HashSet<String> newset = new HashSet<String>();
                newset.add(end);
                eachlevel.put(0, newset);
             }
             
             
             ArrayList<String> lis = new ArrayList<String>();
             helper(eachlevel, level, lis, res);
             return res;
            
        }
        
        private void helper(HashMap<Integer, HashSet<String>> eachlevel, int level, ArrayList<String> lis,  ArrayList     <ArrayList<String>> res ) {
            if (level < 0) {
                res.add(new ArrayList<String>(lis));
                return;
            }
            HashSet<String> set3 = eachlevel.get(level);
            if (set3 == null) {
                return;
            }
            for (String s: set3) {
                lis.add(s);
                helper(eachlevel, level-1, lis, res);
                lis.remove(s);
            }
        }
    
    }

Log in to reply
 

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