Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5148362

Browse filesBrowse files
committed
using bfs to find levels from beginword and dfs to find all paths possible
1 parent 5ef7ede commit 5148362
Copy full SHA for 5148362

File tree

1 file changed

+85
-0
lines changed
Filter options

1 file changed

+85
-0
lines changed

‎BFS/wordLadder2.java

Copy file name to clipboard
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
class Solution {
2+
/*
3+
We will do a BFS to find neighbours for a word and add which level it is
4+
on wrt to start word. Once we have the map, we can do a dfs from start word
5+
to end word by looping over the neighbours for each word and ensuring they
6+
are one level away till we find the endword. Once endword is found we backtrack.
7+
*/
8+
HashMap<String, Integer> distances;
9+
HashMap<String, List<String>> neighbours;
10+
List<List<String>> res;
11+
HashSet<String> words;
12+
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
13+
distances = new HashMap<>();
14+
neighbours = new HashMap<>();
15+
res = new ArrayList<>();
16+
words = new HashSet<>(wordList);
17+
if (wordList == null || wordList.size() == 0 || !words.contains(endWord)) {
18+
return res;
19+
}
20+
bfs(beginWord, endWord, words);
21+
List<String> path = new ArrayList<>();
22+
path.add(beginWord); // adding beginword to the path
23+
dfs(beginWord, endWord, path);
24+
return res;
25+
}
26+
27+
public void bfs(String beginWord, String endWord, HashSet<String> words) {
28+
if (beginWord.equals(endWord)) return;
29+
Queue<String> q = new LinkedList<>();
30+
q.add(beginWord);
31+
int level = 0;
32+
distances.put(beginWord, level);
33+
while(!q.isEmpty()) {
34+
int s = q.size();
35+
for (int i=0; i<s; i++) {
36+
String curWord = q.poll();
37+
if (curWord.equals(endWord)) return;
38+
List<String> neighbors = getNeighbors(curWord, words);
39+
for(String n: neighbors) {
40+
if (!distances.containsKey(n)) {
41+
distances.put(n, level+1);
42+
q.add(n);
43+
}
44+
}
45+
}
46+
level++;
47+
}
48+
}
49+
public void dfs(String currWord, String endWord, List<String> path) {
50+
if (currWord.equals(endWord)) {
51+
res.add(new ArrayList<>(path));
52+
return;
53+
}
54+
List<String> neighb = getNeighbors(currWord, words);
55+
for(String n: neighb) {
56+
if(distances.containsKey(n)) {
57+
// ensuring we are only moving forward and not going into cycle
58+
// adding the correct next word which is one level ahead
59+
if (distances.get(n) == distances.get(currWord) + 1) {
60+
path.add(n);
61+
dfs(n, endWord, path);
62+
path.remove(path.size() - 1); // backtracking
63+
}
64+
}
65+
}
66+
}
67+
68+
public List<String> getNeighbors(String word, HashSet<String> words) {
69+
char[] wordArr = word.toCharArray();
70+
List<String> neig = new ArrayList<>();
71+
for(int i=0; i<wordArr.length; i++) {
72+
for (char c ='a'; c<'z'; c++) {
73+
char temp = wordArr[i];
74+
wordArr[i] = c;
75+
String new_string = new String(wordArr);
76+
if (words.contains(new_string)) {
77+
neig.add(new_string);
78+
}
79+
wordArr[i] = temp;
80+
}
81+
}
82+
neighbours.put(word, neig);
83+
return neig;
84+
}
85+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.