首页 > 其他分享 >127. 单词接龙

127. 单词接龙

时间:2023-07-17 10:44:23浏览次数:34  
标签:wordList string int modify 单词 接龙 127 endWord newWord

字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列 beginWord -> s1 -> s2 -> ... -> sk:

  • 每一对相邻的单词只差一个字母。
  • 对于 1 <= i <= k 时,每个 si 都在 wordList 中。注意, beginWord 不需要在 wordList 中。
  • sk == endWord
    给你两个单词 beginWord 和 endWord 和一个字典 wordList ,返回 从 beginWord 到 endWord 的 最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0 。
 
示例 1:

输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
输出:5
解释:一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog", 返回它的长度 5。

> BFS代码

class Solution {
public:
int ladderLength(string beginWord, string endWord, vector& wordList) {
// 将vector转成unordered_set,提高查询速度
unordered_set wordSet(wordList.begin(), wordList.end());
// 如果endWord没有在wordSet出现,直接返回0
if (wordSet.find(endWord) == wordSet.end()) return 0;
// 记录word是否访问过
unordered_map<string, int> visitMap; // <word, 查询到这个word路径长度>
// 初始化队列
queue que;
que.push(beginWord);
// 初始化visitMap
visitMap.insert(pair<string, int>(beginWord, 1));

    while(!que.empty()) {
        string word = que.front();
        que.pop();
        int path = visitMap[word]; // 这个word的路径长度
        for (int i = 0; i < word.size(); i++) {
            string newWord = word; // 用一个新单词替换word,因为每次置换一个字母
            for (int j = 0 ; j < 26; j++) {
                newWord[i] = j + 'a';
                if (newWord == endWord) return path + 1; // 找到了end,返回path+1
                // wordSet出现了newWord,并且newWord没有被访问过
                if (wordSet.find(newWord) != wordSet.end()
                        && visitMap.find(newWord) == visitMap.end()) {
                    // 添加访问信息
                    visitMap.insert(pair<string, int>(newWord, path + 1));
                    que.push(newWord);
                }
            }
        }
    }
    return 0;
}

};

**> 双向BFS**

class Solution {
    string s, e;
    unordered_set <string> S;
public:
    int ladderLength(string st, string ed, vector<string>& wordList) {
        //双向bfs减少搜索空间宽度
        this->s = st, this->e = ed;
        for(string & w: wordList) S.insert(w);
        if(!S.count(ed)) return 0;
        int ans = bfs();
        return ans == -1? 0 : ans + 1;
    }
    int bfs(){
        unordered_map <string, int> m1, m2;
        queue <string> q1, q2;
        m1[s] =0, m2[e] = 0;
        q1.push(s); q2.push(e);
        while(q1.size() && q2.size()){
            int t = -1;
            if(q1.size() <= q2.size()){
                t = update(q1, m1, m2);
            }
            else{
                t = update(q2, m2, m1);
            }
            if(t != -1) return t;
        }
        return -1;
    }
    int update(queue <string> & q, unordered_map <string, int> & now, unordered_map <string, int> & other){
        int m = q.size();
        while(m--){
            string begin = q.front(); q.pop();
            string modify;
            int n =  begin.size();
            for(int i = 0; i < n; i++){
                modify = begin; 
                for(int j = 0; j < 26; j++){
                modify[i] = char(j + 'a');
                    if(S.count(modify)){
                        if(now.count(modify)) continue;
                        if(other.count(modify)){
                            return now[begin] + 1 + other[modify];
                        }
                        else {
                            q.push(modify);
                            now[modify] = now[begin] + 1;
                        }
                    }
                }
            }
        }
        return -1;
    }
};

标签:wordList,string,int,modify,单词,接龙,127,endWord,newWord
From: https://www.cnblogs.com/lihaoxiang/p/17559345.html

相关文章

  • 目录-英语单词
    A1:链接2:链接3:链接4:链接5:链接6:链接7:链接8:链接9:链接0:链接1:链接2:链接3:链接4:链接5:链接6:链接7:链接8:链接9:链接0:链接B1:链接2:链接3:链接4:链接5:链接6:链接7:链接8:链接9:链接0:链接C1:链接2:链接3:链接4:链接5:链接6:链接7:链接8:链接9:链接0:链接D1:链接2:链接......
  • HJ27 查找兄弟单词
    1.题目读题HJ27 查找兄弟单词  考查点 2.解法思路 代码逻辑 具体实现 publicclassHJ027{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);String[]str=sc.nextLine().split("\\s+");int......
  • AI学英语,背英语单词
     英语作为全球最广泛使用的语言之一,在全球商业、科研、教育等领域起着至关重要的作用。随着技术的发展,人们对英语的学习方式进行了革新,而OpenAI推出的ChatGPT和GPT-4技术就是学习英语的一种新选择。#ChatGPT和GPT-4:新一代AI辅助学习工具首先,了解ChatGPT和其后续版本GPT-4的基......
  • 1-13 编写一个程序,打印输入中单词长度的直方图
    ArchlinuxGCC13.1.1 202304292023-07-1021:56:28星期一 点击查看代码#include<stdio.h>#defineMAX7#defineMIN0intmain(){intnw[10];intnum=0;intnc=0;intc=0;inti,j=0;for(i=0;i<10;i++){......
  • LeetCode -- 792. 匹配子序列的单词数
     方法1:利用桶的思想同时匹配所有words中的子串(走路写法)把所有首字母相同的子串放入到一个桶中,然后遍历s,对于首字母为s[i]的单词,若其大小为1则res++,否则删掉s[i],并根据s[i+1]放入新的桶中。c++classSolution{public:intnumMatchingSubseq(strings,vecto......
  • N9、Transformer实战-单词预测
    ......
  • Why is 127.0.0.1 used for localhost?
       Whyis127.0.0.1usedforlocalhost?Doesanyoneknowwhythatnumberwaschosen?Althoughit’snotdocumentedanywhere(atleastasfarasIknow),buttherearesomelogicalexplanations.Butbeforegoing......
  • 【剑指Offer】44、反转单词序列
    【剑指Offer】44、反转单词序列题目描述:牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student.aamI”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的......
  • 剑指 Offer 58 - I. 翻转单词顺序
    输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。例如输入字符串"Iamastudent.",则输出"student.aamI"。示例1:输入:"theskyisblue"输出: "blueisskythe"示例2:输入:" helloworld! "输出: "worl......
  • CSS英文单词换行
    问题描述有的时候我们需要在页面上展示英文单词,但是有时单词的字母被独立出来形成不了一个整体。比如:使用element-ui中的el-table解决办法:使用css的一个属性,来根据单词进行换行。:deep(.el-table.cell){word-break:break-word;}......