当前位置:网站首页>leetcode-daily question 1408. The difference between string matching in an array (violent enumeration) and the Index method and the Contains method in Golang
leetcode-daily question 1408. The difference between string matching in an array (violent enumeration) and the Index method and the Contains method in Golang
2022-08-06 16:25:00 【Lin Zhongyi】

题目链接:https://leetcode.cn/problems/string-matching-in-an-array/
思路
方法一、暴力枚举
直接想法
The question asks us to find the substrings of other words that exist in the string array,See the title given to usn的范围是[1,100],So we can use two by brute force enumerationforOne layer of the loop refers to the substring and the other refers to the word that exists in the substring,If found, find the next substring
代码示例
func stringMatching(words []string) (ans []string) {
for i, x := range words{
for j, y := range words{
if i != j && strings.Contains(y, x){
//strings.Index(y, x) >= 0
ans = append(ans, x)
break
}
}
}
return
}
注意:Index方法和ContainsThe method here is the same in speed and efficiency,ContainsThe source code is called directlyIndex方法,So both are essentially the sameIndex方法,IndexThe inner part of the method is actually solved by brute force,但是不同的是,He has lazy and nimble mechanics,Not pure violence,有兴趣的可以去Index的源码看看

复杂度分析
- 时间复杂度:O(n2),其中nIndicates the length of the string array,需要用到两个for循环
- 空间复杂度:O(1),不需要额外申请空间
方法二、暴力枚举
direct thought
这个方法是从leetcode其他ac人看到的,Also brute force enumeration in nature,但是不同的是,First sort the string array in descending order of length,Then you only need to find out whether the substring that is shorter than yourself is included in yourself
代码示例
func stringMatching(words []string) []string {
sort.Slice(words, func(i, j int) bool {
return len(words[i]) < len(words[j])
})
n := len(words)
var res []string
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if strings.Index(words[j], words[i]) >= 0 {
res = append(res, words[i])
break
}
}
}
return res
}

复杂度分析
- 时间复杂度:O(n logn + n2),其中nIndicates the length of the string array,Required to sort an array of stringsO(n logn)的时间,Find substring requiredO(n2)的时间
- 空间复杂度:O(1),不需要额外申请空间
边栏推荐
猜你喜欢

Unstoppable, a major breakthrough in China's chip manufacturing industry chain, 5nm equipment will soon be sent to TSMC

科利转债上市价格预测

13. Implementation of paging loading datasets for SAP ABAP OData service (Paging)
JVM:(三)运行时数据区

HJZS-E002(断电延时)电源监视继电器

13. SAP ABAP OData 服务的分页加载数据集的实现(Paging)

小程序中实现搜索功能

dedecms程序GBK编码挑错插件提交中文被过滤

RTU远程终端控制系统S274

Matplotlib
随机推荐
dedecms程序GBK编码挑错插件提交中文被过滤
直播现状:小程序直播电商解决方案,让直播更高效
Activiti部署文件时,报错org.activiti.bpmn.exceptions.XMLException: cvc-complex-type.2.4.a: 发现了以元素 ‘process‘
面渣逆袭:Redis连环五十二问!三万字+八十图详解 上
网络基础
[C Supplement] Example of the difference between int *a[1], int (*a)[10], int (*a)(int), etc.
【JS高级】ES5标准规范之严格模式详解_08
[C Supplement] Precautions for two-dimensional arrays as formal parameters
织梦文档为待审核稿件引起tag标签文档消失bug修复
[C compilation] collection of error reporting problems
社区人物志 | 朱小力:Doris 社区新鲜力量
分类中解决类别不平衡问题
uniapp高度自适应,image标签中加上 mode=“widthFix“即可
Easyexcel导出 只显示出想要的字段
[源码分享] CI/CD 编译打包发布Qt桌面程序
【GO】win10下go-micro控制台安装
1408. 数组中的字符串匹配
LeetCode: 206. The inversion list - simple
Neuron Newsletter 2022-07|新增非 A11 驱动、即将支持 OPC DA
JVM:(二)类加载子系统