Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BFS Solution added #80

Merged
merged 8 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Java/word-ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
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:

Only one letter can be changed at a time.
Each transformed word must exist in the word list.
Note:

Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same.

Problem Link: https://leetcode.com/problems/word-ladder/
It is a popular interview program which is based on BFS
*/
import java.util.*;
class Solution {
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
HashSet<String> hs=new HashSet<>(wordList);
if(!hs.contains(endWord))
{
return 0;
}
int steps=1;
Queue<String> q=new LinkedList<>();
q.add(beginWord);
while(!q.isEmpty())
{
int count=q.size();
for(int i=0;i<count;i++)
{
String curr=q.poll();
char a[]=curr.toCharArray();
for(int j=0;j<a.length;j++)
{
char temp=a[j];
for(char c='a';c<='z';c++)
{
if(a[j]==c) continue;
a[j]=c;
String test=new String(a);
if(test.equals(endWord)) return steps+1;
if(hs.contains(test))
{
q.add(test);
hs.remove(test);
}
}
a[j]=temp;
}
}
steps++;
}
return 0;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ------------------------- | ----------------- | ---------- | --- | ---- |
| 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 | |
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Java](./Java/NumberOfIslands.java) | O(R \* C) | O(R \* C) | Medium | BFS |
| 200 | [Word Ladder](https://leetcode.com/problems/word-ladder/) | [Java](./Java/word-ladder.java) | O(N^2 \* M) | O(N \* M) | Medium | BFS |

<br/>
<div align="right">
Expand Down