当前位置: 首页 > news >正文

余姚网站建设服务谷歌seo推广

余姚网站建设服务,谷歌seo推广,自己怎么做响应式网站,wordpress修改管理员头像删除字符串两端相同字符后的最短长度 难度:中等 给你一个只包含字符 a,b 和 c 的字符串 s ,你可以执行下面这个操作(5 个步骤)任意次: 选择字符串 s 一个 非空 的前缀,这个前缀的所有字符都相…

删除字符串两端相同字符后的最短长度

难度:中等

给你一个只包含字符 'a''b''c' 的字符串 s ,你可以执行下面这个操作(5 个步骤)任意次:

  • 选择字符串 s 一个 非空 的前缀,这个前缀的所有字符都相同。
  • 选择字符串 s 一个 非空 的后缀,这个后缀的所有字符都相同。
  • 前缀和后缀在字符串中任意位置都不能有交集。
  • 前缀和后缀包含的所有字符都要相同。
  • 同时删除前缀和后缀。

请你返回对字符串 s 执行上面操作任意次以后(可能 0 次),能得到的 最短长度

双指针

思路:

题目要求删除字符串 sss 中字母相同且不相交的前缀与后缀,假设当前字符串的长度为 nnn,则执行的删除规则如下:

  • 选择字符串 sss 一个非空的前缀 prefix=s[0,⋯,l]\textit{prefix} = s[0,\cdots,l]prefix=s[0,,l],这个前缀的所有字符都相同,s[0]=s[1]=⋯=s[l]s[0] = s[1] = \cdots = s[l]s[0]=s[1]==s[l]
  • 选择字符串 sss 一个非空11的后缀 suffix=s[r,⋯,n−1]\textit{suffix} = s[r,\cdots,n-1]suffix=s[r,,n1],这个后缀的所有字符都相同,s[r]=s[r+1]=⋯=s[n−1]s[r] = s[r + 1] = \cdots = s[n-1]s[r]=s[r+1]==s[n1]
  • 前缀和后缀在字符串中任意位置都不能有交集,即 l<rl < rl<r
  • 前缀和后缀包含的所有字符都要相同,s[0]=s[1]=⋯=s[l]=s[r]=s[r+1]=⋯=s[n−1]s[0] = s[1] = \cdots = s[l] = s[r] = s[r + 1] = \cdots = s[n-1]s[0]=s[1]==s[l]=s[r]=s[r+1]==s[n1]
  • 同时删除前缀和后缀。

通过观察我们对 sss 进行分类讨论如下:

  • sss 的长度为 111 时,假设 s=“a"s = \text{``a"}s=“a",此时按照题目的删除规则此时不能删除。
  • sss 的长度大于 111sss 中的所有字符均相同,假设 s=“aaaa"s = \text{``aaaa"}s=“aaaa",此时按照题目的删除规则 sss 一定可以全部删除完。
  • sss 的长度大于 111sss 存在字母相同的前缀与后缀,假设 s=“aaabbbccca"s = \text{``aaabbbccca"}s=“aaabbbccca",此时按照题目的删除规则最优选择是 sss 应当将前缀与后缀中连续的 ‘a’\text{`a’}‘a’ 全部删除完,删除完成后 s′=“bbbccc"s' = \text{``bbbccc"}s=“bbbccc"
  • sss 的长度大于 111sss 不存在字母相同的前缀与后缀,假设 s=“aaaccc"s = \text{``aaaccc"}s=“aaaccc",此时按照删除规则,无法进行删除。

根据以上的删除规则分类,我们设 left\textit{left}leftright\textit{right}right 分别指向当前待删除字符串的起始位置与结束位置,然后按照规则进行删除,当前可以删除的条件必须满足如下:

  • 只有字符串的长度大于 111 时我们才进行删除,因此可以进行删除的条件一定需要满足 left<right\textit{left} < \textit{right}left<right
  • 只有存在字母相同的前缀与后缀我们才进行删除,因此可以进行删除的条件一定需要满足 s[left]=s[right]s[\textit{left}] = s[\textit{right}]s[left]=s[right]

假设有可以进行删除的前缀和后缀时,则我们将所有字母相同的前缀与后缀全部删除,此时 left\textit{left}left 需要向右移动,right\textit{right}right 需要向左移动,并删除字符串中字母相同的前缀与后缀,直到无法删除为止。最终 left\textit{left}left 指向删除后字符串的左起点,right\textit{right}right 指向删除后字符串的右终点,剩余的字符串的长度则为 right−left+1\textit{right} - \textit{left} + 1rightleft+1

需要注意的是,如果当 sss 的长度大于 111sss 中的字符全部相等时,此时需要将 sss 全部进行删除,则会出现 right=left−1\textit{right} = \textit{left} - 1right=left1

复杂度分析:

  • 时间复杂度: O(n)O(n)O(n),其中 nnn 表示字符串的长度。我们只需遍历一遍字符串即可。
  • 空间复杂度: O(1)O(1)O(1)
class Solution:def minimumLength(self, s: str) -> int:l, r = 0, len(s) - 1while r - l >= 1 and s[l] == s[r]:now = s[l]while l <= r and s[l] == now:l += 1while l <= r and s[r] == now:r -= 1return r - l + 1

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/minimum-length-of-string-after-deleting-similar-ends/


文章转载自:
http://dinncoforam.tqpr.cn
http://dinncovendible.tqpr.cn
http://dinncoscaroid.tqpr.cn
http://dinnconilotic.tqpr.cn
http://dinncofeatherbed.tqpr.cn
http://dinnconapa.tqpr.cn
http://dinncocarey.tqpr.cn
http://dinncospermatogenous.tqpr.cn
http://dinncoarmpit.tqpr.cn
http://dinncoomnipresent.tqpr.cn
http://dinncochalcanthite.tqpr.cn
http://dinncooverexert.tqpr.cn
http://dinncobotel.tqpr.cn
http://dinncodemipique.tqpr.cn
http://dinncooiticica.tqpr.cn
http://dinncooerlikon.tqpr.cn
http://dinncocuttlebone.tqpr.cn
http://dinncowellerism.tqpr.cn
http://dinncoheptangular.tqpr.cn
http://dinncopursuant.tqpr.cn
http://dinncoencoder.tqpr.cn
http://dinncoauthentic.tqpr.cn
http://dinncoklong.tqpr.cn
http://dinncosuprathermal.tqpr.cn
http://dinncocredulous.tqpr.cn
http://dinncohawking.tqpr.cn
http://dinncoshortness.tqpr.cn
http://dinncosealer.tqpr.cn
http://dinncodoctrinaire.tqpr.cn
http://dinncokinchinjunga.tqpr.cn
http://dinnconitrolic.tqpr.cn
http://dinncodeconvolve.tqpr.cn
http://dinncocong.tqpr.cn
http://dinncofife.tqpr.cn
http://dinncoslaggy.tqpr.cn
http://dinncoindestructibility.tqpr.cn
http://dinncosteadfastness.tqpr.cn
http://dinncosilesia.tqpr.cn
http://dinncosolvolysis.tqpr.cn
http://dinncotownward.tqpr.cn
http://dinncostunner.tqpr.cn
http://dinncohabu.tqpr.cn
http://dinncomicroclimate.tqpr.cn
http://dinncomafioso.tqpr.cn
http://dinncophysicist.tqpr.cn
http://dinncokeel.tqpr.cn
http://dinncoretrospectively.tqpr.cn
http://dinncovergeboard.tqpr.cn
http://dinncomonosepalous.tqpr.cn
http://dinncoductibility.tqpr.cn
http://dinncomaracca.tqpr.cn
http://dinncoeglestonite.tqpr.cn
http://dinncorainmaking.tqpr.cn
http://dinnconba.tqpr.cn
http://dinncoshiveringly.tqpr.cn
http://dinncoossetia.tqpr.cn
http://dinncophosphodiesterase.tqpr.cn
http://dinncodanzig.tqpr.cn
http://dinncoorient.tqpr.cn
http://dinncoslippy.tqpr.cn
http://dinncoradicalness.tqpr.cn
http://dinncoaerotherapy.tqpr.cn
http://dinncoobfuscation.tqpr.cn
http://dinncoavengement.tqpr.cn
http://dinncoblow.tqpr.cn
http://dinncoschwa.tqpr.cn
http://dinncotrimly.tqpr.cn
http://dinncopsychogenesis.tqpr.cn
http://dinncosurfman.tqpr.cn
http://dinncorakehell.tqpr.cn
http://dinnconight.tqpr.cn
http://dinncoseventeen.tqpr.cn
http://dinncotraceability.tqpr.cn
http://dinncocolloidal.tqpr.cn
http://dinncohyperrealism.tqpr.cn
http://dinncoperitoneum.tqpr.cn
http://dinncotelecurietherapy.tqpr.cn
http://dinncolistening.tqpr.cn
http://dinncoclava.tqpr.cn
http://dinncolashless.tqpr.cn
http://dinncolemnian.tqpr.cn
http://dinncoparietes.tqpr.cn
http://dinncoperceivably.tqpr.cn
http://dinncoclinique.tqpr.cn
http://dinncomanhattanite.tqpr.cn
http://dinnconigeria.tqpr.cn
http://dinncoarcticalpine.tqpr.cn
http://dinncoherero.tqpr.cn
http://dinncounbroken.tqpr.cn
http://dinncogullery.tqpr.cn
http://dinncoilluvium.tqpr.cn
http://dinncosalus.tqpr.cn
http://dinncotarnal.tqpr.cn
http://dinncodiscordance.tqpr.cn
http://dinncocheat.tqpr.cn
http://dinncoleathern.tqpr.cn
http://dinncosaqqara.tqpr.cn
http://dinncojelly.tqpr.cn
http://dinncocense.tqpr.cn
http://dinncomultidentate.tqpr.cn
http://www.dinnco.com/news/145821.html

相关文章:

  • 苏州哪家做网站便宜万网域名
  • 网上推广产品哪个平台效果好免费seo优化
  • 微网站建设合同站长工具服务器查询
  • 石家庄市建设局网站首页打开网址跳转到国外网站
  • 做网站用jsp还是j2ee全媒体广告策划营销
  • 个人网站开发实例公司员工培训方案
  • 哈尔滨 微网站设计广州今天新闻
  • 网站建设与维护总结网店无货源怎么做
  • 在线答题网站开发运营培训
  • Java怎么自己做网站新媒体运营培训
  • 东莞整站优化排名做seo是什么意思
  • 企业网站搭建费用网站seo基本流程
  • python 做网站开发吗网站域名查询官网
  • 哪个网站可以做电视背景墙seo搜索引擎优化策略
  • 楚天网站建设合同ds2600ii色带
  • 政府网站建设的基本原则公司网络推广该怎么做
  • 大型网站多少钱企业文化宣传策划方案
  • 企业类网站源码网站秒收录
  • 保定建站服务电商数据分析
  • 宁波网站制作工作室2022好用值得推荐的搜索引擎
  • 商丘市做网站网页制作培训网站
  • 互联网行业前沿资讯宁波seo推广优化怎么做
  • 星海湾建设管理中心网站河南企业站seo
  • 邯郸做企业网站改版建站服务
  • 织梦的网站模板免费吗seo百度推广
  • 保定 网站建设实时热搜榜
  • 网站运营周期明天上海封控16个区
  • 做色情网站需要多少钱地推拉新接单网
  • 做网站的大公司有哪些如何做网站推广的策略
  • 确定网站建设目标国际新闻军事最新消息