当前位置:网站首页>String longest common prefix
String longest common prefix
2022-07-25 10:31:00 【scwMason】
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 : ["flower","flow","flight"]
Output : "fl"
Example 2:
Input : ["dog","racecar","car"]
Output : ""
explain : Input does not have a common prefix .
explain :
All inputs contain only lowercase letters a-z .
link :https://leetcode-cn.com/problems/longest-common-prefix
analysis
Method 1
It's also my stupid way , Is to select a benchmark string , Then match one by one according to the prefix order , This time complexity has reached O(m*n*N) So it's bad
Method 2
utilize Python The character of string , such as aba,abb,abac Three strings ,ascall The largest size is abb, So this feature shows that there is a loop string from scratch in the comparison process , So we can :
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
min_str=min(strs)
max_str=max(strs)
for i,m in enumerate(min_str):
if m!=max_str[i]:
return min_str[:i]
return min_strMethod 3
It's also the use of python Medium zip Method , use zip The built-in method aligns the left side of the string , Then compress longitudinally , And then use set Method to check the string length after de duplication :
def longestCommonPrefix(self, strs):
if not strs: return ""
ss = list(map(set, zip(*strs))) # because zip Method will finally return the Yuanzu set , So we need to use list()
res = ""
for i, x in enumerate(ss):
x = list(x)
if len(x) > 1:
break
res = res + x[0]
return res Take our input example
["flower","flow","flight"] This set uses zip After method , Namely :
["fff","lll","ooi","wwg"]
So after the weight is removed :
["f","l","oi","wg"]
Therefore, the judgment length is greater than 1 It means that there are different letters .
边栏推荐
- When installing mysql, string the service installation failure > mysql80 startup failure
- Open虚拟专线网络负载均衡
- 10.expect免交互
- Storage, computing and distributed computing (collection and sorting is suitable for Xiaobai)
- Ansible部署指南
- 4.FTP服务配置与原理
- 二、unittest框架主要做什么
- Set creation and common methods
- Bug elements
- 将 conda 虚拟环境 env 加入 jupyter kernel
猜你喜欢

存储、计算、分布式存储篇(收集整理适合小白)

Angr (VIII) -- angr_ ctf

Pytorch 张量列表转换为张量 List of Tensor to Tensor 使用 torch.stack()

Erlang (offline deployment)

4. FTP service configuration and principle

1. Shell programming specifications and variables

使用px2rem不生效

Supervisor部署(离线部署需要提前下载部署包)

数论--约数研究

Number theory -- Research on divisor
随机推荐
6.shell之正则表达式
Attention is all you need paper intensive reading notes transformer
异常处理Exception
PyTorch 对 Batch 中每个样本计算损失 Loss for each sample
存储、计算、分布式存储篇(收集整理适合小白)
4.FTP服务配置与原理
11. Iptables firewall
Angr (V) - angr_ ctf
Bug classification and grading
Deploy master-slave database
数组静态初始化,遍历,最值
js 哈希表 02
Oh my Zsh and TMUX configuration (personal)
Small knowledge of common classes
JS encryption parameter positioning
Salt FAQs
Chrome开发者工具详解
Pytorch calculates the loss for each sample in the batch
Fastdfs offline deployment (Graphic)
Set creation and common methods