LeetCode - Word Ladder

public class Solution {
    public int ladderLength(String start, String end, HashSet<String> dict) {
        // Start typing your Java solution below
        // DO NOT write main() function
        LinkedList<String> dict2 = new  LinkedList<String>();
        java.util.Iterator<String> ite = dict.iterator();
        while (ite.hasNext())
            dict2.add(ite.next());
            
        Queue<String> queue = new LinkedList<String>();
        Queue<Integer> length = new LinkedList<Integer>();
        queue.add(start);
        length.add(1);
        
        while (!queue.isEmpty()) {
            String word = queue.poll();
            int len = length.poll();
            if (canChange(word, end))
                return len+1;
            
            for (int i=0; i<dict2.size(); i++) {
                String item = dict2.get(i);
                if (canChange(word, item)) {
                    if (item.equals(end))
                        return len+1;
                    queue.add(item);
                    length.add(len+1);
                    dict2.remove(i);
                    i--;
                } 
            }

        }
        
        return 0;
    }
    
    public boolean canChange(String start, String stop) {
        int diff = 0;
        for (int i=0; i<start.length(); i++)
            if (start.charAt(i) != stop.charAt(i)) {
                if (diff >= 1)
                    return false;
                else
                    diff++;
            }
        return true;
    }
}


BFS search, can pass only Small Judge


public class Solution {
    public int ladderLength(String start, String end, HashSet<String> dict) {
        // Start typing your Java solution below
        // DO NOT write main() function
        Queue<String> queue = new LinkedList<String>();  
        Queue<Integer> length = new LinkedList<Integer>();  
        queue.add(start);  
        length.add(1);
        while (!queue.isEmpty()) {
            String word = queue.poll();
            int len = length.poll();
            if (canChange(word, end))
                return len+1;
            for (int i=0; i<word.length(); i++)
                for (char c='a'; c<='z'; c++) {
                    if (word.charAt(i) == c)
                        continue;
                    char[] arr = word.toCharArray();
                    arr[i] = c;
                    String str = String.valueOf(arr);
                    if (dict.contains(str)) {
                        queue.add(str);
                        length.add(len+1);
                        dict.remove(str);
                    }
                }
        }
        return 0;
    }
    
    public boolean canChange(String start, String stop) {  
        int diff = 0;  
        for (int i=0; i<start.length(); i++)  
            if (start.charAt(i) != stop.charAt(i)) {  
                if (diff >= 1)  
                    return false;  
                else  
                    diff++;  
            }  
        return true;  
    }
}

Can pass Large Judge, can really long time cost:

Run Status: Accepted!
Program Runtime: 1236 milli secs


你可能感兴趣的:(LeetCode - Word Ladder)