当前位置:网站首页>day10每日3题(3):数组中的字符串匹配
day10每日3题(3):数组中的字符串匹配
2022-06-26 16:17:00 【程序猿不脱发2】
题目:
给你一个字符串数组 words ,数组中的每个字符串都可以看作是一个单词。请你按 任意 顺序返回 words 中是其他单词的子字符串的所有单词。
如果你可以删除 words[j] 最左侧和/或最右侧的若干字符得到 word[i] ,那么字符串 words[i] 就是 words[j] 的一个子字符串。
示例 1:
输入:words = [“mass”,“as”,“hero”,“superhero”]
输出:[“as”,“hero”]
解释:“as” 是 “mass” 的子字符串,“hero” 是 “superhero” 的子字符串。
[“hero”,“as”] 也是有效的答案。
示例 2:
输入:words = [“leetcode”,“et”,“code”]
输出:[“et”,“code”]
解释:“et” 和 “code” 都是 “leetcode” 的子字符串。
示例 3:
输入:words = [“blue”,“green”,“bu”]
输出:[]
提示:
1 <= words.length <= 100
1 <= words[i].length <= 30
words[i] 仅包含小写英文字母。
题目数据 保证 每个 words[i] 都是独一无二的。
通过
思路:
双重遍历, 尽量减少里层遍历的次数。
java代码:
class Solution {
public List<String> stringMatching(String[] words) {
List<String> res = new ArrayList<>();
for (int i = 0; i < words.length-1; i++) {
for (int j = i+1; j < words.length; j++) {
if (!res.contains(words[j]) && words[i].contains(words[j])) {
res.add(words[j]);
} else if (!res.contains(words[i]) && words[j].contains(words[i])) {
res.add(words[i]);
}
}
}
return res;
}
}
边栏推荐
- R语言使用cor函数计算相关性矩阵进行相关性分析,使用corrgram包可视化相关性矩阵、行和列使用主成分分析重新排序、下三角形中使用平滑的拟合线和置信椭圆,上三角形中使用散点图、对角线最小值和最大值
- [from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
- # 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
- R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
- Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law
- Common properties of XOR and addition
- Solidus labs welcomes zhaojiali, former head of financial innovation in Hong Kong, as a strategic adviser
- Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
- 6 custom layer
- 架构实战营毕业设计
猜你喜欢

Ideal path problem

Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl

基于 MATLAB的自然过渡配音处理方案探究

当一个程序员一天被打扰 10 次,后果很惊人!
![[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul](/img/26/c0c8a406ff4ffe0ae37d277f730bd0.png)
[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul

stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题

Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged

清华“神奇药水”登Nature:逆转干细胞分化,比诺奖成果更进一步,网友:不靠精子卵子就能创造生命了?!...

(1) Keras handwritten numeral recognition and recognition of self written numbers

Angel 3.2.0 new version released! Figure the computing power is strengthened again
随机推荐
牛客小白月赛50
Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system
当一个程序员一天被打扰 10 次,后果很惊人!
NFT transaction principle analysis (2)
手写数字体识别,用保存的模型跑自己的图片
首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...
R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
基于STM32+华为云IOT设计的云平台监控系统
IAR工程适配GD32芯片
4 自定义模型训练
pybullet机器人仿真环境搭建 5.机器人位姿可视化
Kept to implement redis autofailover (redisha)
Redis 迁移(操作流程建议)
心情不好,我就这样写代码
今年高考英语AI得分134,复旦武大校友这项研究有点意思
Redis的ACID
【时间复杂度和空间复杂度】
[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap
牛客编程题--必刷101之动态规划(一文彻底了解动态规划)