Skip to content

Commit c250c24

Browse files
authored
BFS Solution added (#80)
* Added the file * Added word-ladder program * Update README.md
1 parent adb3d9e commit c250c24

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Java/word-ladder.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
3+
4+
Only one letter can be changed at a time.
5+
Each transformed word must exist in the word list.
6+
Note:
7+
8+
Return 0 if there is no such transformation sequence.
9+
All words have the same length.
10+
All words contain only lowercase alphabetic characters.
11+
You may assume no duplicates in the word list.
12+
You may assume beginWord and endWord are non-empty and are not the same.
13+
14+
Problem Link: https://leetcode.com/problems/word-ladder/
15+
It is a popular interview program which is based on BFS
16+
*/
17+
import java.util.*;
18+
class 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+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
254254
| ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------- | ----------------- | ---------- | --- | ---- |
255255
| 1284 | [Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/) | [C++](./C++/Minimum-Number-of-Flips-to-Convert-Binary-Matrix-to-Zero-Matrix.cpp) | _O(m * n * 2 ^ (m \* n))_ | _O(2 ^ (m \* n))_ | Hard | BFS | |
256256
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Java](./Java/NumberOfIslands.java) | O(R \* C) | O(R \* C) | Medium | BFS |
257+
| 200 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Java](./Java/word-ladder.java) | O(N^2 \* M) | O(N \* M) | Medium | BFS |
257258

258259
<br/>
259260
<div align="right">

0 commit comments

Comments
 (0)