Word Ladder题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/word-ladder/description/


Description

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:

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

For example,

Given:

beginWord = "hit"

endWord = "cog"

wordList = ["hot","dot","dog","lot","log","cog"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",

return its length 5.

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.

UPDATE (2017/1/20):

The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

Solution

class Solution {
private:
set<string> isVisited;
map<string, string> preVertex;
public:
bool canTrans(string a, string b) {
int count = 0;
for (int i = 0; i < a.length(); i++)
if (a[i] != b[i]) {
if (count == 0)
count++;
else
return false;
}
return count == 1;
} int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
if (wordList.size() == 0)
return 0;
isVisited.insert(beginWord);
queue<string> vq;
vq.push(beginWord);
string qfront;
bool finish = false;
while (!finish && !vq.empty()) {
qfront = vq.front();
vq.pop();
for (auto& w: wordList) {
if (canTrans(qfront, w) && isVisited.count(w) == 0) {
isVisited.insert(w);
vq.push(w);
preVertex[w] = qfront;
if (w == endWord) {
finish = true;
break;
}
}
}
}
if (finish) {
int len = 1;
string v = endWord;
while (v != beginWord) {
v = preVertex[v];
len++;
}
return len;
} else {
return 0;
}
}
};

解题描述

这道题我采用的是BFS去查找指定的终点。同时用一个map来记录每一个已经被搜索的顶点的前一个点。

[Leetcode Week5]Word Ladder的更多相关文章

  1. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  2. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  3. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  4. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  5. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  6. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  7. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  8. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  9. [Leetcode][JAVA] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

随机推荐

  1. [转]Hibernate入门:批量插入数据

    转自:http://blog.csdn.net/xiazdong/article/details/7709068 一般如果要插入100万条数据,则会写如下代码: package org.xiazdon ...

  2. JavaScript Map数据结构

    Array.prototype.remove = function (s) { for (var i = 0; i < this.length; i++) { if (s == this[i]) ...

  3. 全局 Ajax 事件处理器

    jQuery中将Ajax请求和响应分成了若干(5)个阶段 并且允许开发者在Ajax请求和响应的不同阶处理不同的逻辑, 这些方法用于注册事件处理器,用来处理页面上的任何 Ajax 请求,当某些事件触发后 ...

  4. JVM GC Q&A(补充ing)

    1.如果一个对象没有与其相连的GC ROOT,一定会被回收吗? 这个对象并非是非死不可的,这时他只是处于死缓阶段,要真正宣告一个对象的死亡,至少要经历两次标记过程:如果对象在进行可达性分析后发现并没有 ...

  5. Lyft Level 5 Challenge 2018 - Final Round Div. 1没翻车记

    夜晚使人着迷.没有猝死非常感动. A:显然对于水平线段,只有横坐标的左端点为1的时候才可能对答案产生影响:对于竖直直线,如果要删一定是删去一段前缀.枚举竖直直线删到哪一条,记一下需要删几条水平线段就可 ...

  6. BZOJ4444 SCOI2015国旗计划(贪心+倍增)

    链上问题是一个经典的贪心.于是考虑破环成链,将链倍长.求出每个线段右边能作为后继的最远线段,然后倍增即可. #include<iostream> #include<cstdio> ...

  7. NS产品演进

    NS产品演进 Citrix产品体系================ Citrix产品类别================ NS产品演进================ 产品联系方式========== ...

  8. 【C++ troubleshooting】A case about decltype

    template <typename iter_t> bool next_permutation(iter_t beg, iter_t end) { // if (beg == end | ...

  9. 2018牛客多校第三场 C.Shuffle Cards

    题意: 给出一段序列,每次将从第p个数开始的s个数移到最前面.求最终的序列是什么. 题解: Splay翻转模板题.存下板子. #include <bits/stdc++.h> using ...

  10. Substrings Sort string 基本操作

    You are given nn strings. Each string consists of lowercase English letters. Rearrange (reorder) the ...