当前位置:网站首页>Longest common prefix and
Longest common prefix and
2022-07-03 07:18:00 【darabiuz】
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string “”.
Example 1:
Input :strs = [“flower”,“flow”,“flight”]
Output :“fl”
Example 2:
Input :strs = [“dog”,“racecar”,“car”]
Output :""
explain : Input does not have a common prefix .
// First assume the longest public prefix ans Is the first , Traverse each subsequent string , And ans Go through one by one , Record subscript if different , Intercept ans return
var longestCommonPrefix = function (strs) {
if (!strs.length) return ''
let ans = strs[0]
// Traversal string array
for (let i = 1; i < strs.length; i++) {
// Traversing every character of a string
let tmp
if (!strs[i].length) return ''
for (let j = 0; j < strs[i].length; j++) {
// and ans contrast , Different ends convenience , here j It's the next one in a different position index
if (ans[j] != strs[i][j]) {
tmp = j
break
}
tmp = j + 1
}
ans = ans.substr(0, tmp)
}
return ans
};
longestCommonPrefix(["abab", "aba", ""])
边栏推荐
- centos php7.2.24升级到php7.3
- 1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
- 【已解决】Unknown error 1146
- Inno Setup 制作安装包
- 【无标题】
- MySQL transaction rollback, error points record
- Inno setup production and installation package
- How can I split a string at the first occurrence of “-” (minus sign) into two $vars with PHP?
- The difference between typescript let and VaR
- MySQL mistakenly deleted the root account and failed to log in
猜你喜欢

Win 10 find the port and close the port

在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储

Practice of enterprise ab/testing platform

docker建立mysql:5.7版本指定路径挂载不上。

Liang Ning: 30 lectures on brain map notes for growth thinking

Sorting out the core ideas of the pyramid principle

Le Seigneur des anneaux: l'anneau du pouvoir

Pits encountered in the use of El checkbox group

Dora (discover offer request recognition) process of obtaining IP address

My 2020 summary "don't love the past, indulge in moving forward"
随机推荐
[untitled]
[attribute comparison] defer and async
高并发内存池
【最詳細】最新最全Redis面試大全(50道)
The underlying mechanism of advertising on websites
C代码生产YUV420 planar格式文件
[set theory] partition (partition | partition example | partition and equivalence relationship)
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
C WinForm framework
Distributed transactions
php artisan
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
万卷书 - 价值投资者指南 [The Education of a Value Investor]
Visit Google homepage to display this page, which cannot be displayed
Setting up the development environment of dataworks custom function
7.2刷题两个
Laravel frame step pit (I)
Sorting out the core ideas of the pyramid principle
Advanced API (UDP connection & map set & collection set)
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!