leetcode 84: Word Ladder

Word LadderFeb 11

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

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

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

 

uncompleted

public class Solution {
    
    private int _step = 0;
    
    public int ladderLength(String start, String end, HashSet<String> dict) {
        // Start typing your Java solution below
        // DO NOT write main() function
        int sz1 = start.length();
        int sz2 = end.length();
        if(sz1==0 || sz1!=sz2) return 0;
        _step = Integer.MAX_VALUE;
        
        
        if(start.equals(end) ) return 1;
        else if(dict.isEmpty() ) return 0;
        
        HashSet<String> temp = new HashSet<String>();
        ladRec( start, end, dict, 0, temp);
        return _step==Integer.MAX_VALUE ? 0 :  _step+1;
    }
    
    private void ladRec(String start, String end, HashSet<String> dict, int level, HashSet<String> temp) {
        if(start.equals( end) ) {
            if(level<_step) _step=level;
            return;
        }
        
        if( level >=_step ) return;
    
        for(int i=0; i<start.length(); i++) {
            if(start.charAt(i)!=end.charAt(i)) {
                for(String str : findNext( start,  i, dict) ){
                    if( !temp.contains( str ) ) {
                        temp.add( str );
                        ladRec(str, end, dict, i+1,temp);
                        temp.remove(temp.size() );
                    }
                } 
            }
        }
    }
    
    private ArrayList<String> findNext( String start, int i, HashSet<String> dict) {
        ArrayList<String> res = new ArrayList<String>();
        StringBuilder sb = new StringBuilder(start);
        for( char c = 'a'; c<='z';c++) {
            sb.setCharAt(i, c);
            if( dict.contains(sb.toString())){
                res.add( sb.toString() );
            }
        } 
        return res;
    }
}

 

 


 

public class Solution {
    public int ladderLength(String start, String end, HashSet<String> dict) {
        // Start typing your Java solution below
        // DO NOT write main() function
        
        int sz1 = start.length();
        int sz2 = start.length();
        int path = 0;
        if(sz1 != sz2) return 0;
        if(start.equals(end) ) return 2;
        
        Queue<String> queue = new LinkedList<String>();
        
        queue.offer( start );
        
        HashSet<String> hitted = new HashSet<String>();
        hitted.add(start);
        
        int l1 = 1;
        int l2 = 0;
        //BFS
        while( !queue.isEmpty() ) {
            String s = queue.poll();
            l1--;
            HashSet<String> strs = nextStr(s,dict, hitted);
            l2 += strs.size();
                  
            for(String str : strs ) {
                if( str.equals( end ) ) {
                    return path+2;
                } else {
                    queue.offer( str );
                }
            }
            
            if(l1==0) {
                ++path;
                l1=l2;
                l2=0;
            }
        }
        
        return 0;
    }
    
    private HashSet<String> nextStr(String s, HashSet<String> dict, HashSet<String> hitted) {
        HashSet<String> strs = new HashSet<String>();
        StringBuilder sb = new StringBuilder(s);
        for(int i=0; i<s.length(); i++) {
            for(char c='a'; c<='z'; c++) {
                 sb.setCharAt(i,c);
                 String t = sb.toString();
                 if( dict.contains( t ) && !hitted.contains( t) ) {
                    hitted.add( t);
                    strs.add( t );
                 }
            }
        }
        
        return strs;
    } 
}

public class Solution {
    public int ladderLength(String start, String end, HashSet<String> dict) {
        // Start typing your Java solution below
        // DO NOT write main() function
        
        int sz1 = start.length();
        int sz2 = start.length();
        int path = 0;
        if(sz1 != sz2) return 0;
        if(start.equals(end) ) return 2;
        
        Queue<String> queue = new LinkedList<String>();
        
        queue.offer( start );
        
        HashSet<String> hitted = new HashSet<String>();
        hitted.add(start);
        
        int l1 = 1;
        int l2 = 0;
        //BFS
        while( !queue.isEmpty() ) {
            String s = queue.poll();
            l1--;
            HashSet<String> strs = nextStr(s,dict, hitted);
            l2 += strs.size();
                  
            for(String str : strs ) {
                if( str.equals( end ) ) {
                    return path+2;
                } else {
                    queue.offer( str );
                }
            }
            
            if(l1==0) {
                ++path;
                l1=l2;
                l2=0;
            }
        }
        return 0;
    }
    
    private HashSet<String> nextStr(String s, HashSet<String> dict, HashSet<String> hitted) {
        HashSet<String> strs = new HashSet<String>();
        
        for(int i=0; i<s.length(); i++) {
            StringBuilder sb = new StringBuilder(s);
            for(char c='a'; c<='z'; c++) {
                 sb.setCharAt(i,c);
                 String t = sb.toString();
                 if( dict.contains( t ) && !hitted.contains( t) ) {
                    hitted.add( t);
                    strs.add( t );
                 }
            }
        }
        
        return strs;
    } 
}


 


posted @ 2013-02-21 07:44  西施豆腐渣  阅读(181)  评论(0编辑  收藏  举报