0127 Word Ladder
Last updated
Last updated
My Solution:
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> beginSet = new HashSet<>();
Set<String> endSet = new HashSet<>();
beginSet.add(beginWord);
endSet.add(endWord);
Set<String> dict = new HashSet<>(wordList);
if(!dict.contains(endWord)) return 0;
return search(beginSet, endSet, dict, true, 1);
}
private int search(Set<String> beginSet, Set<String> endSet, Set<String> dict, boolean isForward, int cnt){
if(beginSet.isEmpty() || endSet.isEmpty()) return 0;
cnt++;
dict.removeAll(beginSet);
Set<String> nextSet = new HashSet<>();
for(String str : beginSet){
char[] chs = str.toCharArray();
for(int i = 0; i < chs.length; i++){
char c = chs[i];
for(char j = 'a'; j <= 'z'; j++){
chs[i] = j;
String tmp = new String(chs);
if(!dict.contains(tmp)) continue;
if(endSet.contains(tmp)) return cnt;
nextSet.add(tmp);
}
chs[i] = c;
}
}
return nextSet.size() > endSet.size() ? search(endSet, nextSet, dict, false, cnt) : search(nextSet, endSet, dict, true, cnt);
}
}