当前位置: 首页 > news >正文

LeetCode刷题---哈希表---648

单词替换

648. 单词替换 - 力扣(LeetCode)

题目

在英语中,我们有一个叫做 词根(root) 的概念,可以词根 后面 添加其他一些词组成另一个较长的单词——我们称这个词为 衍生词 (derivative)。例如,词根 help,跟随着 继承词 "ful",可以形成新的单词 "helpful"

现在,给定一个由许多 词根 组成的词典 dictionary 和一个用空格分隔单词形成的句子 sentence。你需要将句子中的所有 衍生词 用 词根 替换掉。如果 衍生词 有许多可以形成它的 词根,则用 最短 的 词根 替换它。

你需要输出替换之后的句子。

示例 1:

输入:dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
输出:"the cat was rat by the bat"

示例 2:

输入:dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
输出:"a a b c"

提示:

  • 1 <= dictionary.length <= 1000
  • 1 <= dictionary[i].length <= 100
  • dictionary[i] 仅由小写字母组成。
  • 1 <= sentence.length <= 106
  • sentence 仅由小写字母和空格组成。
  • sentence 中单词的总量在范围 [1, 1000] 内。
  • sentence 中每个单词的长度在范围 [1, 1000] 内。
  • sentence 中单词之间由一个空格隔开。
  • sentence 没有前导或尾随空格。

自己的思路和代码

思路:

        我们用set来存储dictionary中的所有的词根,这样可以保证有顺序,然后遍历这个句子,将含有词根的单词返回词根,否则返回该单词就可以了。

代码:
class Solution {
public:
    string replaceWords(vector<string>& dictionary, string sentence) {
        set<string> table;
        string result = "";
        for(int i=0; i<dictionary.size(); i++) {
            table.insert(dictionary[i]);
        }
        string temp = "";
        int s = 0;
        bool flag = false;
        while(s < sentence.size()) {
            if(sentence[s] != ' ') {
                temp += sentence[s];
                if(table.find(temp) == table.end()) {
                    if(s==sentence.size()-1) result = result + temp + ' ';
                    s++;
                } else {
                    result = result + temp + ' ';
                    temp = "";
                    flag = true;
                    while((s+1) < sentence.size() &&  sentence[s+1] != ' ') {
                        s++;
                    }
                    s++;
                }
            } else {
                if(flag) {
                    flag = false;
                    s++;
                } else {
                    result = result + temp + ' ';
                    s++;
                    temp = "";
                }
            }
        }

        // for(auto it=table.begin(); it!=table.end(); it++) {
        //     printf("%s\n", (*it).c_str());
        // }
        result.pop_back();
        return result;
    }
};

相关文章:

  • 基于springboot校园健康系统的设计与实现(源码+文档)
  • deepseek:推荐一个免费没有广告的电脑桌面备忘录
  • TikTok账户安全指南:如何取消两步验证?
  • easyexcel 2.2.6版本导出excel模板时,标题带下拉框及其下拉值过多不显示问题
  • 【Python爬虫(27)】探索数据可视化的魔法世界
  • Pytorch实现之ISRGAN高分辨率图像生成训练自己的低分辨率图像
  • 3dmax噪波制作镜头震动动画
  • 【网络编程】TCP连接connect几次syn之后一直返回EINVAL问题
  • 使用Geotools读取DEM地形数据实战-以湖南省30米数据为例
  • 模电知识点总结(4)
  • leetcode:3110. 字符串的分数(python3解法)
  • 深入学习解析:183页可编辑PPT华为市场营销MPR+LTC流程规划方案
  • 成员函数定义后面加const是什么功能:C++中const成员函数的作用
  • DP-最长上升子序列
  • 【git】提交修改、回撤、回滚、Tag 操作讲解,与reset (--soft、--mixed、--hard) 的区别
  • sharpGL的openGL兼容性
  • Python的那些事第二十七篇:Python中的“数据魔法师”NumPy
  • 安利:外文文献翻译插件
  • Eclipse自动排版快捷键“按了没有用”的解决办法
  • 正则表达式–断言
  • “80后”李岩已任安徽安庆市领导
  • 生态环境法典草案拟初审:应对气候变化等问题将作原则性规定
  • 商务部谈中欧汽车谈判进展
  • 我国成年国民综合阅读率82.1%,数字化阅读接触率首超80%
  • 嫦娥五号月球样品将借给这些国家机构
  • 富力地产:广州富力空港假日酒店第一次拍卖流拍,起拍价约2.77亿元