[LeetCode]Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

使用广搜:

由于要记录路径因此要注意两点

1:注意判重

2:使用一个树记录路径一般使用 Map<String,List<String>>();


public class Solution {
	Set<String> used =  new HashSet<>();
	List<List<String>> res = new ArrayList<>();
	List<String> path = new ArrayList<>();
	public List<List<String>> findLadders(String start, String end, Set<String> dict){
		Queue<String> current = new LinkedList<>();
		Queue<String> next = new LinkedList<>();
		Map<String,List<String>> father = new HashMap<>();
		current.add(start);
		boolean found = false;
		while(!current.isEmpty()&&!found){
			for(String s:current){
				used.add(s);
			}
			while(!current.isEmpty()){
				String string = current.poll();
				char []str = string.toCharArray();
				for(int i=0;i<str.length;i++){
					char co = str[i];
					for(char ch ='a';ch<'z';ch++){
							if(ch == co) continue;
							str[i] = ch;
							String newStr = new String(str);
							if((dict.contains(newStr)||end.equals(newStr))&&!used.contains(newStr)){
								next.offer(newStr);
								List<String> path = father.get(newStr);
								if(path==null){
									path = new ArrayList<>();
									path.add(string);
								}else{
									if(!path.contains(string))
									path.add(string);
								}
								father.put(newStr, path);
								if(end.equals(newStr)) found = true;
							}
						}
						str[i] = co;
					}
				}
				if(found) break;
				Queue<String> temp = current;
				current = next;
				next = temp;
		}
		if(!found) return res;
		path.add(end);
		genRes(start,path,father,end);
		return res;
	}
	 
 	private void genRes(String start,List<String> path,Map<String,List<String>> father,String str){
 		if(str.equals(start)){
 			List<String> list = new ArrayList<>(path);
 			Collections.reverse(list);
 			res.add(list);
 			return;
 		}
 		List<String> lin = father.get(str);
 		for(int i=0;i<lin.size();i++){
 			String s = lin.get(i);
			path.add(s);
 			genRes(start,path,father,s);
 			path.remove(path.size()-1);
 		}
 	}
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值