class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> set = new HashSet<>(wordList);
if (set.size() == 0 || !set.contains(endWord)) {
return 0;
}
set.remove(beginWord);
Queue<String> q = new LinkedList<>();
q.offer(beginWord);
int step = 1;
while (!q.isEmpty()) {
int qSize = q.size();
for (int i = 0; i < qSize; i++) { //BFS固定寫法
String cur = q.poll();
for (int j = 0; j < cur.length(); j++) {
for (char c = 'a'; c <= 'z'; c++) { // 一個一個換字
char[] charArray = cur.toCharArray();
charArray[j] = c;
String newWord = String.valueOf(charArray);
if (set.contains(newWord)) { // 比對是否存在
if (newWord.equals(endWord)) { // 找到 endword 回傳
return step + 1;
}
q.offer(newWord); // 其他狀況 繼續跑
set.remove(newWord); // 移掉找到過的字
}
}
}
}
step++;
}
return 0;
}
}
char[] charArray = cur.toCharArray(); // 要這時後取出
for (char c = 'a'; c <= 'z'; c++) {
char[] charArray = cur.toCharArray();
charArray[j] = c;
String newWord = String.valueOf(charArray);
if (set.contains(newWord)) {
if (newWord.equals(endWord)) {
return step + 1;
}
q.offer(newWord);
set.remove(newWord);
}
}
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if (wordList == null || wordList.size() == 0) return 0;
// use bfs, change word to try to meet wordlist
HashSet<String> set = new HashSet<>(wordList);
set.remove(beginWord);
Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
int step = 1;
while (!queue.isEmpty()) {
int qSize = queue.size();
for (int q = 0; q < qSize; q++) {
String cur = queue.poll();
if (cur.equals(endWord)) return step; // terminator, if reach endWord
for (int j = 0; j < cur.length(); j++) {
for (char c = 'a'; c <= 'z'; c++) { // 一個一個換字
char[] charArray = cur.toCharArray();
charArray[j] = c;
String newWord = String.valueOf(charArray);
if (set.contains(newWord)) { // 比對是否存在
queue.offer(newWord); // 其他狀況 繼續跑
set.remove(newWord); // 移掉找到過的字
}
}
}
}
step++;
}
return 0;
}
}
T: O(26*M^2) , M: word_len , N wordList_size, M^2 because new String also needs M
constructing a new string (String.valueOf(cAry)) inside the loop is
O(M) because it involves creating a new character array and copying
M characters into it
S: O(N), set size
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Set<String> set = new HashSet<>(wordList);
if (!set.contains(endWord)) { // there is a edge case
return 0;
}
Queue<String> queue = new LinkedList<>();
queue.offer(beginWord);
int steps = 1; // beginWord is already 1
while (!queue.isEmpty()) {
int size = queue.size();
for (int q = 0; q < size; q++) {
String cur = queue.poll();
if (endWord.equals(cur)) {
return steps;
}
for (int i = 0; i < cur.length(); i++) {
for (char ch = 'a'; ch <= 'z'; ch++) {
char cha[] = cur.toCharArray();
cha[i] = ch;
String newStr = new String(cha);
if (set.contains(newStr)) {
queue.offer(newStr);
set.remove(newStr); //remember to remove
}
}
}
}
steps++;
}
return 0; // cant find endWord from beginWord's transform
}
}
/*
do BFS
hit for each char to replace with 26 chars
if hit wordList put to queue
termial case is equal endWord
*/
for (int i = 0; i < cur.length(); i++) {
for (char ch = 'a'; ch <= 'z'; ch++) {
char cha[] = cur.toCharArray();