Skip to content

Instantly share code, notes, and snippets.

@SuryaPratapK
Created September 23, 2020 16:17
Show Gist options
  • Save SuryaPratapK/e8947c55e83b67c4c9da555bc67f101d to your computer and use it in GitHub Desktop.
Save SuryaPratapK/e8947c55e83b67c4c9da555bc67f101d to your computer and use it in GitHub Desktop.
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;
}
};
@ankurgupta255
Copy link

Hey, I think lines 46 to 50 should be swapped with 44 to 45. Because I think we should first check if the word which we are forming exists in the set that we initially created. Doing this lead to a significant reduction in the runtime on leetcode. Please correct me if I am wrong...

@harishKandikatla
Copy link

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;

         Queue<String> q = new LinkedList<>();
    q.add(beginWord);
    int depth = 0;
    
    while(!q.isEmpty())
    {
        depth+=1;
        int lsize = q.size();   //No of elements at a level
        while(lsize-- > 0)
        {
            String curr = q.poll();
            //check for all possible 1 depth words
            for(int i=0;i<curr.length();++i)  //For each index
            {
                StringBuilder temp1 = new StringBuilder(curr);
                for(char c='a';c<='z';++c)  //Try all possible chars
                {
                    temp1.setCharAt(i, c);
                    String temp = temp1.toString();
                    
                    if(curr.compareTo(temp) == 0)
                        continue;   //Skip the same word
                    if(temp.compareTo(endWord) == 0)
                        return depth+1; //endWord found
                    if(myset.contains(temp))
                    {
                        q.add(temp);
                        myset.remove(temp);
                    }
                }
            }
        }
    }
    return 0;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment