Python最长公共子串

方法一

最简单最容易想到的方法,去数组第一个元素为最长公共前缀,如果是,就return,如果不是就减去最后一个单词。只到找到位置。

class Solution:def longestCommonPrefix(self, strs):""":type strs: List[str]:rtype: str"""if len(strs) ==0:return ''prefix = strs[0]for i in range(1,len(strs)):while prefix != strs[i][0:len(prefix)]:prefix = prefix[0:len(prefix)-1]if prefix == '':return ''return prefix

改进版:

class Solution:def longestCommonPrefix(self, strs):""":type strs: List[str]:rtype: str"""if len(strs) == 0:return ""prefix = strs[0]for s in strs:while s.find(prefix) != 0:prefix = prefix[0:len(prefix) - 1]if prefix == "":return prefixreturn prefix

方法二

从第一单词的第一个字母往后加,算法速度和前面差不多。

class Solution:def longestCommonPrefix(self, strs):""":type strs: List[str]:rtype: str"""if len(strs)== 0:return ''prefix =''for i in range(len(strs[0])):prefix += strs[0][i]for x in strs:if x.find(prefix)!=0:return prefix[0:len(prefix)-1]return prefix

方法三

分治法,于排序算法中归并排序的思想类似。

class Solution:def longestCommonPrefix(self, strs):""":type strs: List[str]:rtype: str"""if len(strs) == 0:return ''return self.longest_prefix(strs, 0, len(strs)-1)def longest_prefix(self,strs, I, r):if I == r:return strs[I]else:mid = (I + r) // 2lcpL = self.longest_prefix(strs, I, mid)lcpR = self.longest_prefix(strs, mid + 1, r)return self.commonprefix(lcpL, lcpR)def commonprefix(self,lcpL, lcpR):Min = min(len(lcpL), len(lcpR))for i in range(Min):if lcpL[i] != lcpR[i]:return lcpL[0:i]return lcpL[0:Min]

第四种

二分法:



由此可以看出,二分法还是快的呀!!!

class Solution:def longestCommonPrefix(self, strs):""":type strs: List[str]:rtype: str"""if len(strs) == 0:return ''Min = 2**32for i in strs:Min = min(Min,len(i))low, high = 1, Minwhile low <= high:mid = (low + high) //2if self.iscommonPrefix(strs,mid):low = mid + 1else:high = mid - 1return strs[0][0:(low+high)//2]def iscommonPrefix(self,strs,length):str1 = strs[0][0:length]for i in range(1,len(strs)):if strs[i].find(str1)!=0:return Falsereturn True

Python最长公共子串相关推荐

  1. python -- 最长公共子串

    #! /usr/bin/env python3 # -*- coding: utf-8 -*-# 对于最长公共子串问题,答案为网格中最大的数字--它可能并不位于最后的单元格中def findlonge ...

  2. java实现最长连续子序列_最长公共子序列/最长公共子串 Python/Java实现

    关注我的微信公众号:后端技术漫谈 不定期推送关于后端开发.爬虫.算法题.数据结构方面的原创技术文章,以及生活中的逸闻趣事. 我目前是一名后端开发工程师.主要关注后端开发,数据安全,网络爬虫,物联网,边 ...

  3. Algorithm:C++/python语言实现之求旋转数组最小值、求零子数组、求最长公共子序列和最长公共子串、求LCS与字符串编辑距离

    Algorithm:C++/python语言实现之求旋转数组最小值.求零子数组.求最长公共子序列和最长公共子串.求LCS与字符串编辑距离 目录 一.求旋转数组最小值 1.分析问题 2.解决思路 二.求 ...

  4. 用Python计算最长公共子序列和最长公共子串

    如何用Python计算最长公共子序列和最长公共子串 1. 什么是最长公共子序列?什么是最长公共子串? 1.1. 最长公共子序列(Longest-Common-Subsequences,LCS) 最长公 ...

  5. python实现编辑距离,最长公共子序列,最长公共子串

    python实现编辑距离 python实现最大公共子序列 python实现最长公共子串

  6. 【236】Python求列表最长字符串及lambda和最长公共子串

    ♣ 题目部分(原文见公众号:python宝) python宝: https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU5Nj ...

  7. LCS/最长公共子序列/最长公共子串 实现 Python/Java

    参考 http://blog.csdn.net/u012102306/article/details/53184446 http://blog.csdn.net/hrn1216/article/det ...

  8. [Python]获取2个字符串的最长公共子串

    原创文章,欢迎转载.转载请注明:转载自 祥的博客 原文链接:https://blog.csdn.net/humanking7/article/details/84645055 文章目录 @[toc] ...

  9. Python —— 查询两个字符串的最长公共子串

    查询两个字符串的最长公共子串 查询两个字符串的最长公共子串 查询两个字符串的最长公共子串 思路: 1.通过字符串1从全长开始判断是否存在于字符串2中,如果不存在则迭代至只有1位字符 2.通过列表来保存 ...

最新文章

  1. Windows下svn客户端和服务器的安装使用
  2. 史上最强轻量级人脸检测,全面超越retinaface
  3. 【OpenGL】九、OpenGL 绘制基础 ( OpenGL 状态机概念 | OpenGL 矩阵概念 )
  4. maven学习笔记之IDEA+Maven+Jetty运行一个简单的web项目
  5. datagrid如何获取一行数据中的某个字段值_MySQL 如何查找删除重复行?
  6. Python教程:Python如何实现穷举搜索?
  7. HttpClient, 使用C#操作Web
  8. python工作环境_CentOS7下python工作环境管理
  9. 电脑下载的M4A格式文件怎么转换为MP3格式
  10. 百度搜索引擎优化指南3.0_深圳网站搜索引擎排名优化电话,百度优化排名费用_华阳网络...
  11. Oracle 不同故障的恢复方案
  12. C++基础之类的定义和对象的创捷,什么是类和对象?
  13. 在ubuntu 64位的机器上执行arm-linux-gcc提示 no such file or directory
  14. PHP字符串函数hex2bin( 转换十六进制字符串为二进制字符串)
  15. 2018美赛ABCDEF题翻译
  16. 网易卡搭python_网易卡搭编程
  17. Fcitx使用搜狗词库与皮肤
  18. 2022年劳务员-通用基础(劳务员)考试题库及答案
  19. php 生成指定长度字符串
  20. Python常用STL

热门文章

  1. 适合入门自学服装裁剪滴书(更新ing)
  2. fortran使用MKL函数库计算一个复数向量的共轭与另一个复数向量的内积
  3. 公司开发网站怎么对接短信平台?
  4. RedHat AS4-U2下Mysql 5.0的集群配置
  5. 移动端JQ插件hammer使用详解
  6. Hadoop的FileInputFormat解析
  7. 破冰、融合、同心 —— 沃创云开展2021年户外团建活动
  8. python变量和常量_004Python变量和常量
  9. Halcon中已知一条直线计算它的垂线与延长线
  10. Airbnb背后周下载量数百万的Eslint-Airbnb