MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / ladderLength

Method ladderLength

Java/word-ladder.java:19–56  ·  view source on GitHub ↗
(String beginWord, String endWord, List<String> wordList)

Source from the content-addressed store, hash-verified

17import java.util.*;
18class Solution {
19 public int ladderLength(String beginWord, String endWord, List<String> wordList) {
20 HashSet<String> hs=new HashSet<>(wordList);
21 if(!hs.contains(endWord))
22 {
23 return 0;
24 }
25 int steps=1;
26 Queue<String> q=new LinkedList<>();
27 q.add(beginWord);
28 while(!q.isEmpty())
29 {
30 int count=q.size();
31 for(int i=0;i<count;i++)
32 {
33 String curr=q.poll();
34 char a[]=curr.toCharArray();
35 for(int j=0;j<a.length;j++)
36 {
37 char temp=a[j];
38 for(char c='a';c<='z';c++)
39 {
40 if(a[j]==c) continue;
41 a[j]=c;
42 String test=new String(a);
43 if(test.equals(endWord)) return steps+1;
44 if(hs.contains(test))
45 {
46 q.add(test);
47 hs.remove(test);
48 }
49 }
50 a[j]=temp;
51 }
52 }
53 steps++;
54 }
55 return 0;
56 }
57}

Callers

nothing calls this directly

Calls 3

addMethod · 0.45
isEmptyMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected