LeetCode 127 Word Ladder

题目


给出两个字符串和一个字典,判断两个字符串通过这个字典,需要多少步骤.


代码1



public int ladderLength(String beginWord, String endWord, 
		 Set<String> wordDict) {
    	int[] ans =new int[1];
    	ans[0]=Integer.MAX_VALUE;
    	int temp =1;
    	Object[] strArray =wordDict.toArray();
    	
    	boolean[] used = new boolean[strArray.length];
    	useme(beginWord,endWord,strArray,used,ans,temp);
    	return ans[0] == Integer.MAX_VALUE?0:ans[0];   
    }


     private  void useme(String beginWord, String endWord, Object[] strArray,
			boolean[] used, int[] ans, int temp) {
		
    	 if(oneStep(beginWord,endWord)){
    		 ans[0]=Math.min(temp+1,ans[0]);
    		 return;
    	 }
    	 
    	 if(temp>beginWord.length()+2){
    	     return;
    	 }
    	 
    	 for(int i =0;i<strArray.length;i++){
    		 String cur = (String)strArray[i];
    		 if(!used[i] && oneStep(beginWord,cur)){
    			 used[i]=true;
    			 useme(cur,endWord,strArray,used,ans,temp+1);
    			 used[i]=false;
    		 }
    	 }
	}


	private boolean oneStep(String beginWord, String cur) {
		if(beginWord.length()!=cur.length()){
			return false;
		}
		int count = 0;
		for(int i=0;i<beginWord.length();i++){
			if(beginWord.charAt(i)!=cur.charAt(i)){
				count++;
			}
		}
		return count==1?true:false;
	}

代码2


public class Solution {
     public int ladderLength(String beginWord, String endWord, 
   		 Set<String> wordDict) {
    	LinkedList<String> queue = new LinkedList<String>();
    	HashMap<String,Integer> record = new HashMap<String,Integer>();
    	queue.add(beginWord);
    	record.put(beginWord, 1);
    	if(oneStep(beginWord, endWord)){
    		return 2;
    	}
    	while(!queue.isEmpty()){
    		String cur = queue.poll();
    		int count = record.get(cur);
    		
    		for(int i=0;i<cur.length();i++){
    			for(char change ='a';change<='z';change++){
    				if(cur.charAt(i)==change){
    					continue;
    				}
    				StringBuffer sb = new StringBuffer(cur);
    				sb.setCharAt(i, change);
    				String changedCur = sb.toString();
    				if(changedCur.equals(endWord)){
    					return count+1;
    				}
    				if(wordDict.contains(changedCur)&& !record.containsKey(changedCur)){
    					queue.add(changedCur);
    					record.put(changedCur, count+1);
    				}
    			}
    		}
    	}
    	return 0;
    }
   	private  boolean oneStep(String beginWord, String cur) {
   		if(beginWord.length()!=cur.length()){
   			return false;
   		}
   		int count = 0;
   		for(int i=0;i<beginWord.length();i++){
   			if(beginWord.charAt(i)!=cur.charAt(i)){
   				count++;
   			}
   		}
   		return count==1?true:false;
   	}
}

思考


1 典型的bfs。路径是字符串之间一个字符的改变,同时新字符串在字典中。

2 一种方式是每次都到字典里的所有字符串寻找,如代码1. 一种方式每次更改原字符串的一个字符,看是否在字典中,如代码2

3 算法分析一下。如果字典有n个string,每个string大小 l 长度 ,平均每个string关联k个另外的string。那么方法一需要O(k*k*n)的复杂度。方法二需要O(k*l*26)。根据k*n和I*26  来选择对应方法。

4 leetcode 的案列 应用的是 n大 l小的案列,所以用后者。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值