Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: soapyigu/LeetCode-Swift
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: anshu231999/LeetCode-Swift
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Oct 15, 2020

  1. Update WordLadder.swift

    anshu231999 authored Oct 15, 2020
    Copy the full SHA
    ea315b6 View commit details
Showing with 42 additions and 0 deletions.
  1. +42 −0 BFS/WordLadder.swift
42 changes: 42 additions & 0 deletions BFS/WordLadder.swift
Original file line number Diff line number Diff line change
@@ -48,3 +48,45 @@ class WordLadder {
return 0
}
}
//c++ code of word ladder
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
set<string> s;
for(int i=0;i<wordList.size();i++)
{
s.insert(wordList[i]);
}
if(s.find(endWord)==s.end())
return 0;
int level=1;
queue<string> q;
q.push(beginWord);
while(!q.empty())
{
level++;
int siz=q.size();
for(int i=0;i<siz;i++)
{
string word=q.front();
q.pop();
for(int pos=0;pos<beginWord.size();pos++)
{
char orichar=word[pos];
for(char c='a';c<='z';c++)
{
word[pos]=c;
if(word==endWord)
return level;
if(s.find(word)==s.end())
continue;
s.erase(word);
q.push(word);
}
word[pos]=orichar;
}
}
}
return 0;
}
};