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. 1.1.2 ADO.NET模型
  2. lstm代码_只需5行代码!LSTM时间序列建模以及预测
  3. window下的Django环境搭建
  4. C#正则表达式判断字符串中是否有数…
  5. keras系列︱keras是如何指定显卡且限制显存用量(GPU/CPU使用)
  6. Google搜索 - 世界各国Google网址大全
  7. 网络安全扫盲贴 史上最全网络安全问题解答
  8. 软件工程——软件维护总结
  9. 管理:49个终身受用的职场“顶级思维”!
  10. php获取微博热搜,爬取微博热搜top50(示例代码)
  11. 别不信,小宝宝爱盯着妈妈看原来跟大脑发育有关
  12. 解决Pycharm中下载不了sklearn问题
  13. 支付与对账业务的流程
  14. js将字符串按照逗号分割
  15. OpenGL入门二——变换
  16. 整理iOS 错误警告
  17. 大学本科毕业生如何免费进行论文查重
  18. 读书笔记-天才和凡人
  19. Python轻松抓取微信公众号文章
  20. 求长方形的周长和面积c语言两个函数_C语言及程序设计.第四课.项目2.计算长方形周长和面积...

热门文章

  1. Android不同Module之间Activity跳转(以腾讯IMDemo为例)
  2. python个人信息管理系统登录注册增加日程_个人信息管理系统网站设计论文模板.docx...
  3. 重磅消息Apache Flink 1.9.0隆重发布
  4. SugarCRM商业客户关系管理软件介绍
  5. 用C语言写一个简单的贪吃蛇游戏(用到easyx图形库)
  6. LINUX ULE112 系列筆記
  7. 尚观学习-ule-权限
  8. 丑拒!如何在Python中编写精美图形界面
  9. 当皮卡丘长出绒毛,“丑拒”还是“真香”都在猫眼短评里了。|无用但有趣
  10. python 使用 openpyxl 处理 Excel 教程