Question for word ladder


  • 1
    X

    Here is my code. Could anyone help me point out what's wrong with my code?
    Thanks.

        public class Solution {
    	 public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
    	       ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
    	       ArrayList<ArrayList<String>> newlist = new ArrayList<ArrayList<String>>();
    	       ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
    	       ArrayList<String> layer = new ArrayList<String>();
    	       HashSet<String> set = new HashSet<String>();
    	       if(!dict.contains(end)){
    	            return list;     
    	       }
    	       layer.add(start);
    	       set.add(start);
    	       if(start == end){
    	           layer.add(end);
    	           list.add(layer);
    	           return list;
    	       }
    	       list.add(layer);
    	       while(list.size() != 0){
    	           for(int m = 0; m < list.size(); m++){
    	               ArrayList<String> previous = list.get(m);
    	               int size = previous.size();
    //get the last word from previous level, use it as next start word
    	               String temp = previous.get(size - 1);
    //change each character in the word from  a to z
    	               for(int j = 0; j < temp.length(); j++){
    	                       String left = temp.substring(0, j);
    	                       String right = temp.substring(j+1, temp.length());
    
    	                       for(char replace = 'a'; replace <= 'z'; replace++){
    	                           ArrayList<String> copyprevious = new ArrayList<String>();
    //copy the whole previous path
    	                           for(int count = 0; count < previous.size(); count++){
    	                               copyprevious.add(previous.get(count));
    	                           }
    
    	                          String next = left + replace + right;
    	                          if(dict.contains(next)){
    	                              if(set.contains(next)){
    	                                    if(next.equals(end)){
    //copyprevious used to update current path
    	                                        copyprevious.add(next);
    //newlist used to keep track of the change of the path
    	                                        newlist.add(copyprevious);
    //result used for save all the possible result path
    //add each final path to result
    	                                        result.add(copyprevious);
    	                                    }
    	                               }
    	                               else{
    	                                   copyprevious.add(next);
    	                                   newlist.add(copyprevious);
    	                                   set.add(next);
    	                                   if(next.equals(end)){
    	                                        result.add(copyprevious);
    	                                    }
    	                               }
    	                            }
    	                       }
    	                }	           
    	           }
    	           if(result.size() != 0){
    	        	   return result;
    	           }
    	           list = newlist;
    	           newlist = new ArrayList<ArrayList<String>>();  
    	       }
    	       return result;
    	 }
    }

  • 0
    S

    Please format your code correctly, select all code then click {} button.


  • 0
    L

    If you want people to read your code, you need to make more efforts in the presentation. No one is able to understand easily a code where there are 5 nested loops and 3 nested ifThenElse. Cut it into small functions. You might even figure out your problem yourself doing that!


  • 0
    X

    I know where is wrong with my code...thank you~~!


Log in to reply
 

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