Created
September 23, 2020 16:17
-
-
Save SuryaPratapK/e8947c55e83b67c4c9da555bc67f101d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int speedUp=[](){ | |
std::ios::sync_with_stdio(false); | |
cin.tie(nullptr); | |
cout.tie(nullptr); | |
return 0; | |
}(); | |
class Solution { | |
public: | |
int ladderLength(string beginWord, string endWord, vector<string>& wordList) { | |
unordered_set<string> myset; | |
bool isPresent = false; //Checks if endWord is present in Dict | |
//Insert all words from Dict in myset | |
for(auto word: wordList) | |
{ | |
if(endWord.compare(word)==0) | |
isPresent = true; | |
myset.insert(word); //Insert word in Dict | |
} | |
if(isPresent==false) //endWord is not present in Dict | |
return 0; | |
queue<string> q; | |
q.push(beginWord); | |
int depth = 0; | |
while(!q.empty()) | |
{ | |
depth+=1; | |
int lsize = q.size(); //No of elements at a level | |
while(lsize--) | |
{ | |
string curr = q.front(); | |
q.pop(); | |
//check for all possible 1 depth words | |
for(int i=0;i<curr.length();++i) //For each index | |
{ | |
string temp = curr; | |
for(char c='a';c<='z';++c) //Try all possible chars | |
{ | |
temp[i] = c; | |
if(curr.compare(temp)==0) | |
continue; //Skip the same word | |
if(temp.compare(endWord)==0) | |
return depth+1; //endWord found | |
if(myset.find(temp)!=myset.end()) | |
{ | |
q.push(temp); | |
myset.erase(temp); | |
} | |
} | |
} | |
} | |
} | |
return 0; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
java solution
public class Solution {
public int solve(String beginWord, String endWord, String[] wordList) {
Set myset = new HashSet();
boolean isPresent = false; //Checks if endWord is present in Dict
//Insert all words from Dict in myset
for(String word: wordList)
{
if(endWord.compareTo(word) == 0)
isPresent = true;
myset.add(word); //Insert word in Dict
}
if(isPresent==false) //endWord is not present in Dict
return 0;
}