检查字符串中的字母(Check letters in the string)

编程入门 行业动态 更新时间:2024-10-20 07:36:37
检查字符串中的字母(Check letters in the string)

如何检查字符串中的前三个字母是否与最后三个字母相反。 它们不能重叠。 例如:

"karak" -> False "kaakaak" -> True

想出这样的话,我可以查看前三个和后三个反转的字母串:

if s[:3] == s[-3:][::-1]:

但是如何检查它们是否重叠?

How to check if the first three letters in the string are the same as three last letters reversed. They can't overlap. For example:

"karak" -> False "kaakaak" -> True

Figured out that like this I can check out first three and last three reversed letters of string:

if s[:3] == s[-3:][::-1]:

But how to check if they are overlaping or not?

最满意答案

检查长度并将前3个字母与最后3个字母进行比较:

is_equal = True if len(s) >= 6 and s[:3] == s[-3:][::-1] else False

Checking length and compare first 3 letters with last 3 letters:

is_equal = True if len(s) >= 6 and s[:3] == s[-3:][::-1] else False

更多推荐

本文发布于:2023-07-16 13:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128980.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字母   Check   letters   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!