LeetCode 472. Concatenated Words

AI工程化之生成式UI & A2UI(二) 本文介绍了生成式UI框架A2UI的架构设计与核心实现。项目采用AI协作开发模式,通过ATDD(AI驱动的测试驱动开发)确保代码质量。前端SDK基于zustand实现框架无关的状态管理,包含协议解析器、存储管理、渲染器等核心模块。重点展示了基础store构建、A2UI协议解析器的实现过程,包括mock数据准备、测试用例生成和可视化调试。项目采用monorepo管理,利用vite实现高效开发构建。整个系统设计遵循模块化原则,通过清晰的接口定义实现各组件解耦,为后续功能扩展打下基础。 阅读详情

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words.
A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.

Example:

Input: [“cat”,”cats”,”catsdogcats”,”dog”,”dogcatsdog”,”hippopotamuses”,”rat”,”ratcatdogcat”]

Output: [“catsdogcats”,”dogcatsdog”,”ratcatdogcat”]

Explanation: “catsdogcats” can be concatenated by “cats”, “dog” and “cats”;
“dogcatsdog” can be concatenated by “dog”, “cats” and “dog”;
“ratcatdogcat” can be concatenated by “rat”, “cat”, “dog” and “cat”.

Note:

The number of elements of the given array will not exceed 10,000
The length sum of elements in the given array will not exceed 600,000.
All the input string will only include lower case letters.
The returned elements order does not matter.

这题是找到一堆字符串中,一些有其他字符串连接而成的字符串,如 “dogcatsdog” 是由 “dog”, “cats” 和 “dog”连接而成的。

这个题的Note给了比较多的信息,第一个是规模不大于10,000,第二个是累计长度不超过600,000,第三个是字符串都是小写,第四个是最终结果的顺序可以随意。

这个题目主要是看字符串是否是由其他字符串连接而成,为了减少计算量,自然而然,可以想到几点:
1.一个字符串只能由比它长度短的字符串连接而成。那么如果按长度排序,则可以减少计算量。
2.如果一个字符串前n个字符与另外一个字符串完全相同,那么只要看剩下的部分是不是连接而成的就可以了。
3.利用类似桶排序的思想,进一步优化,将首字母相同的字符串放入同一个集合中,这会使得比较次数减少。(其实和微软2014实习生在线测试题String Reorder那种想法是差不多的,String Reorder比较简单一些)

另外,注意以下两点:(也是我WA的地方)
1.”dog”不是由”dog”连接而成的,否则所有的都是连接而成的字符串。(-.-一开始没有想到)。
2.测试数据中存在空字符串(”“)。

isConcatenated是用一个dfs的方法,遍历与要判断的字符串首字母相同的字符串集合(长度从小到大),如果存在一个字符串和要判断的字符串的前n个完全一样,则将要判断的字符串前n个字符删去,继续调用isConcatenated。其中times,是记录要判断字符串是由几个字符串连接而成,对代码稍作修改还能返回对应组成它的字符串集合。

步骤:
1.按长度排序
2.桶排序思想,按字符串首字母分类,并去除(“”)
3.调用isConcatenated
4.返回结果

方法比较简单,也比较暴力,代码如下:

class Solution {
public:
    vector<string> v[26];
    vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
        vector<string> ans;
        vector<string>::iterator it;
        vector<string>::iterator vis;

        sort(words.begin(), words.end(),[](string &a, string &b){
            return a.size() < b.size();
        });
        for (it=words.begin(); it!=words.end(); it++) {
            if((*it)==""){
                vis = it;
                it --;
                words.erase(vis);
                continue;
            }
            int num = (*it)[0]-'a';
            v[num].push_back(*it);
        }

        for (it = words.begin(); it!=words.end(); it++) {
            int num=(*it)[0]-'a';
            if(isConcatenated((*it), v[num], 0)){
                ans.push_back(*it);
            }
        }
        return ans;
    }

    bool isConcatenated(string target,vector<string>& words,int times){
        vector<string>::iterator vis;
        bool ans=false;

        for (vis = words.begin(); vis!=words.end(); vis++) {
            if((*vis).size()==target.size()) {
                if(times&&(*vis)==target) return true;
                else continue;
            }
            else if((*vis).size()>target.size()) break;
            else{
                if ((*vis)==target.substr(0,(*vis).size())){
                    if (target.size()-(*vis).size()==0) {
                        ans=true;
                        break;
                    }else{
                        int num = target.substr((*vis).size())[0]-'a';
                        ans = isConcatenated(target.substr((*vis).size()), v[num],times+1);
                        if(ans)
                            break;
                    }
                }
            }
        }
        return ans;
    }
};
用Arduino和RC522模块DIY一个NFC门禁卡复制器(附完整代码) 本文详细介绍了如何利用Arduino和RC522模块DIY一个NFC门禁卡复制器,包括硬件连接、软件配置、数据读写技术及完整代码实现。通过本教程,电子爱好者可以深入了解NFC技术,并构建自己的智能门禁系统,适用于家庭安全、办公场所等多种场景。 阅读详情

相关推荐

跨越移植陷阱:ESP-IDF下LVGL与ILI9341的组件化集成与常见避坑指南

本文详细解析了在ESP-IDF环境下将LVGL图形库与ILI9341显示屏驱动进行组件化集成的完整流程。重点介绍了移植过程中的关键配置策略、内存管理优化技巧以及常见问题解决方案,帮助开发者有效规避内存溢出、显示异常等典型陷阱,实现高效稳定的嵌入式GUI开发。

yhn456789的博客 599

【字典树】leetcode472.连接词

题目: 给你一个 不含重复 单词的字符串数组 words ,请你找出并返回 words 中的所有 连接词 。 连接词 定义为:一个完全由给定数组中的至少两个较短单词组成的字符串。 思路: 字典树+DFS 解答: class Trie: def __init__(self): #children代表字符是否存在 self.children = [None] * 26 #标识该节点是不是终止节点 self.isEnd = False

jqq125的博客 271

【TC397第23篇】深度解析TC397 EB MCAL开发系列:UART配置与POLL模式数据交互

通过本文,我们深度解析了TC397 EB MCAL开发系列中UART配置的POLL模式下的数据交互。从技术要点的介绍到实际案例的演示,希望读者能够更好地理解UART在POLL模式下的配置与应用。这不仅是对嵌入式系统开发的一次深刻探索,更是为项目提供了一种高效通信的解决方案。在未来的文章中,我们将继续深入研究嵌入式系统开发的各个方面,为广大开发者提供更多实用的技术知识。敬请期待,一同迎接嵌入式技术的新篇章!

一直在水些技术小文 448

LeetCode472 题:连接词(C++)

472. 连接词 - 力扣(LeetCode) 和LeetCode第 140 题:单词拆分 II(C++)_zj-CSDN博客相似,但是做法不太一样。 本题我用的是字典树+dfs: class Trie{ public: struct TrieNode{ bool isEnd = false; TrieNode* next[26] = {NULL}; }; Trie() : root(new TrieNode) {} void insert(

zj 316

LeetCode472 连接词 dfs + 哈希set

题目描述 给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词。 连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的。 示例: 输入: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"] 输出: ["catsdogcats","dogcats...

AKGWSB 's blog 277

LeetCode刷题(简单程度)】720. 词典中最长的单词

给出一个字符串数组words组成的一本英语词典。从中找出最长的一个单词,该单词是由words词典中其他单词逐步添加一个字母组成。若其中有多个可行的答案,则返回答案中字典序最小的单词。 若无答案,则返回空字符串。 示例 1: 输入: words = [“w”,“wo”,“wor”,“worl”, “world”] 输出:“world” 解释: 单词"world"可由"w", “wo”, “wor”, 和 "worl"添加一个字母组成。 示例 2: 输入: words = [“a”, “banana”, “ap

qq_33197518的博客 516

leetcode 472. Concatenated Words

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entirel...

cmy203的博客 306

LeetCode472Concatenated Words

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entir

mattmu的博客 411

LeetCode //C - 472. Concatenated Words

【代码】LeetCode //C - 472. Concatenated Words

Made in Code 1043

Leetcode472. Concatenated Words

题目地址: https://leetcode.com/problems/concatenated-words/ 给定一个非空英文小写字符串组成的数组AAA,题目保证字符串两两不同,返回其所有能被至少两个别的字符串拼接而成的字符串。

数学、算法爱好者的博客 384

leetcode472. Concatenated Words

题目如下: Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is comprise...

weixin_33971205的博客 180

[Leetcode] 472. Concatenated Words 解题报告

题目: Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is compri

魔豆(Magicbean)的博客 1681

[leetcode]472. Concatenated Words

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entirely...

lianyhai 236

LeetCode472 Concatenated Words

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entirely...

Tech in Pieces 298

LeetCode472. Concatenated Words

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is comprised entirel...

李歇特冯-兹拜因巴哈的博客 208

Leetcode 472. Concatenated Words

问题描述: Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. A concatenated word is defined as a string that is compr

u010370157的博客 417

易语言源码视频捕获模块.zip

易语言源码视频捕获模块.zip

大华摄像头工具-抓拍、录像 daHuaCameraTool.rar

大华摄像头工具-抓拍、录像 daHuaCameraTool.rar 博客地址:https://blog.csdn.net/lw112190/article/details/150544134

上一篇: LeetCode 14. Longest Common Prefix
下一篇: LeetCode 11. Container With Most Water
Singlerush
博客等级 码龄9年 5粉丝 29原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值