127 Word Ladder

127 Word Ladder

Time 
https://www.youtube.com/watch?v=vWPCm69MSfs

https://zxi.mytechroad.com/blog/searching/127-word-ladder/


Bfs one direction 
// wrong result 



class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
      int len = beginWord.length();
      int result = 0;
      Set<String> set = new HashSet<>();
      for(String word : wordList){
        set.add(word);
      }
      
      if(!set.contains(endWord)){
        return result;
      }
      
      Queue<String> queue = new LinkedList<>();
      queue.offer(beginWord);
      while(!queue.isEmpty()){
        int size = queue.size();
        result++;
        for(int s = 0; s < size; s++){
          String p = queue.poll();
          char[] current = p.toCharArray();
          
          for(int i = 0; i < len; i++){
            char c = current[i]; // hot , i = 0, c = h 
            for (char ch = 'a'; ch <= 'z'; ch++) { ///
              if(ch == c){
                continue;
              }else{
                current[i] = ch;
                String newString = new String(current); //// new an object 
                if(newString == endWord){// compare string, use equals , == compares address 
                  return result;
                }
                if(!set.contains(newString)){
                  continue;
                }else{
                  queue.offer(newString);
                  set.remove(newString);
                }
              }
            }
            current[i] = c;
          }
        }
      }
      return 0;
    }
    
}
    






/// bfs bi direction 

// still wrong result 


class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
      Set<String> set = new HashSet<>();
      for(String word : wordList){
        set.add(word);
      }
      if(!set.contains(endWord)){
        return 0;
      }
      
      Set<String> q1 = new HashSet<>();
      Set<String> q2 = new HashSet<>();
      
      q1.add(beginWord);
      q2.add(endWord);
      
      int l = beginWord.length();
      
      int steps = 0;
      
      while(q1.isEmpty() && !q2.isEmpty()){
        steps++;
        
        if(q1.size() > q2.size()){
          Set<String> tmp = q1; /// swap, generating the smaller queue first 
          q1 = q2;
          q2 = tmp;
        }
        
        Set<String> q = new HashSet<>();
        
        for(String word : q1){
          char[] array = word.toCharArray();
          for(int i = 0; i < l; i++){
            char ch = array[i];
            for( char c  = 'a'; c <= 'z'; c++){
              if(c == ch) continue;
              array[i] = c;
              String newString = new String(array);
              if(q2.contains(newString)){
                return steps + 1;
              }
              if(!set.contains(newString)){
                continue;
              }
              set.remove(newString);
              q.add(newString);
            }
            array[i] = ch;
          }
        }
        q1 = q;
      }
      return 0;
    }
}

 

// correct 
class Solution {
    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        Set<String> dict = new HashSet<>();
        for(String word : wordList) dict.add(word);
        if(!dict.contains(endWord)) return 0;
        Queue<String> queue = new LinkedList<>();
        queue.offer(beginWord);
        int level = 1;
        
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i = 0; i < size; i++){
                String curWord = queue.poll();
                char[] curArray = curWord.toCharArray();
                for(int j = 0; j < curArray.length; j++){
                    char c = curArray[j];
                    for(char ch = 'a'; ch <= 'z'; ch++){
                        if(ch == c){
                            continue;
                        }else{
                            curArray[j] = ch;
                            String newWord = new String(curArray);
                            if(newWord.equals(endWord)) return level + 1;
                            if(!dict.contains(newWord)) continue;
                            dict.remove(newWord);
                            queue.offer(newWord);
                        }
                    }
                    curArray[j] = c;
                }
            }
            level++;
            
        }
        return 0;

    }
}

 

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Example 2:

Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

posted on 2018-08-10 14:39  猪猪&#128055;  阅读(143)  评论(0)    收藏  举报

导航