检查字符串中的字母(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" -> TrueFigured 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 FalseChecking 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更多推荐
发布评论