From 5c05e0476627a5707779db7b72a532ac158cd442 Mon Sep 17 00:00:00 2001 From: ATRI2107 Date: Fri, 9 Oct 2020 23:17:26 +0530 Subject: [PATCH 1/3] Added the file --- Java/word-ladder.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Java/word-ladder.java diff --git a/Java/word-ladder.java b/Java/word-ladder.java new file mode 100644 index 00000000..e69de29b From 73cfef37cfd2578ecffe69d36326198d4897e1f1 Mon Sep 17 00:00:00 2001 From: ATRI2107 Date: Fri, 9 Oct 2020 23:22:11 +0530 Subject: [PATCH 2/3] Added word-ladder program --- Java/word-ladder.java | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/Java/word-ladder.java b/Java/word-ladder.java index e69de29b..ac66c80c 100644 --- a/Java/word-ladder.java +++ b/Java/word-ladder.java @@ -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 wordList) { + HashSet hs=new HashSet<>(wordList); + if(!hs.contains(endWord)) + { + return 0; + } + int steps=1; + Queue q=new LinkedList<>(); + q.add(beginWord); + while(!q.isEmpty()) + { + int count=q.size(); + for(int i=0;i Date: Sat, 10 Oct 2020 21:11:26 +0530 Subject: [PATCH 3/3] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1ddd6a1a..416d5a26 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,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 |