Ejemplo n.º 1
0
 //If there is no '&' before set, the time cost will be tripled;
 int ladderLength(unordered_set<string>& bList, unordered_set<string>& eList, unordered_set<string>& wordList, int len)
 {
     if(bList.empty())
         return 0;
     if(bList.size() > eList.size())
         return ladderLength(eList, bList, wordList, len);
     unordered_set<string> tmpList;
     for(auto it=bList.begin(); it!=bList.end(); it++)
     {
         string cur = *it;
         for(int i=0; i<cur.size(); i++)
         {
             char c0 = cur[i];
             for(char c='a'; c<='z'; c++)
             {
                 cur[i]=c;
                 if(eList.count(cur)) return len+1;
                 if(wordList.count(cur))
                 {
                     tmpList.insert(cur);
                     wordList.erase(cur);
                 }
             }
             cur[i] = c0;
         }
     }
     return ladderLength(tmpList, eList, wordList, len+1);
 }
Ejemplo n.º 2
0
    int check(string beginWord, string endWord, unordered_set<string> wordList) 
    {
        if(beginWord == endWord) return 2;

        auto n = wordList.find(beginWord);

        if(n != wordList.end()) wordList.erase(n);

        int res = 9999;

        for (int i = 0; i < beginWord.size(); ++i)
        {
            string s = beginWord;
            for(char j = 'a'; j <= 'z'; j++)
            {
                s[i] = j;
                if(s == endWord) return 2;
                auto pos = wordList.find(s);
                if (pos != wordList.end())
                {
                    wordList.erase(pos);
                    res = min(res,ladderLength(s,endWord,wordList));
                }
            }
        }
        return res;
    }
Ejemplo n.º 3
0
    vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
        unordered_map<string, vector<string*> > prev;
        int len = ladderLength(start, end, dict, prev);

        vector<vector<string> > result;
        vector<string*> path;
        setResult(start, end, prev, path, result);
        return result;
    }
Ejemplo n.º 4
0
TEST(WordLadderTest, T1) {
	std::string output;
	testing::internal::CaptureStdout();

	std::vector<std::string> wordList{ "hot","dot","dog","lot","log" };
	auto result = ladderLength("hit", "cog", wordList);

	output = testing::internal::GetCapturedStdout();
	ASSERT_EQ(0, result);
}
Ejemplo n.º 5
0
 vector<vector<string> > findLaddersII(string start, string end, unordered_set<string> &dict) {
     vector<vector<string> > result;
     vector<string> ret;
     int minLen = ladderLength(start, end, dict);
     if (!minLen) {
         return result;
     }
     ret.push_back(start);
     dfs(ret, minLen, end, dict, result);
     return result;
 }
Ejemplo n.º 6
0
 int ladderLength(string bWord, string eWord, unordered_set<string>& wordList)
 {
     if(bWord == eWord)
         return 1;
     unordered_set<string> bList, eList;
     bList.insert(bWord);
     eList.insert(eWord);
     wordList.erase(bWord);
     wordList.erase(eWord);
     return ladderLength(bList, eList, wordList, 1);
 }
Ejemplo n.º 7
0
/*
int ladderLength(string start, string end, unordered_set<string> & dict)
{
	if(start.size() != end.size()) return 0;
	if(isConnect(start, end)) return 2;

	int res = dict.size() + 3;
	for (std::unordered_set<string>::iterator itr = dict.begin(); itr != dict.end(); ++itr)
	{
		if(isConnect(start, *itr))
		{
			unordered_set<string> tem = dict;
			tem.erase(*itr);
			int t = ladderLength(*itr, end, tem);
			if(t != 0) res = min(res, 1 + t);
		}
	}
	if(res == dict.size() + 3) return 0;
	return res;
}
*/
int main(int argc, char const *argv[])
{
	string start = "hit";
	string end = "cog";
	unordered_set<string> dict = {"hot","cog","dot","dog","hit","lot","log"};
	cout<<start <<endl <<end<<endl;
	for (std::unordered_set<string>::iterator i = dict.begin(); i != dict.end(); ++i)
	{
		cout<<*i<<",";
	}
	cout<<endl;

	cout<<ladderLength(start, end, dict)<<endl;
	return 0;
}