【每日一题0723】偶串长度

:woman_mage:如果一个字符串由两个相同字符串连接而成,就称这个字符串是偶串。例如"xyzxyz"和"aaaaaa"是偶串,但是"ababab"和"xyzxy"却不是。

现在给你一个只包含小写字母的偶串s,你可以从字符串s的末尾删除1个或者多个字符,保证删除之后的字符串还是一个偶串。请编写一个函数,返回删除之后得到最长偶串长度。

示例:
输入:abaababaab
输出:6

题目难度:中等
题目来源:牛客网-偶串

def find_even(s:str):
    pass
    
assert find_even('abaababaab') == 6
def find_even(str):
    lis = list(str)
    while True:
        lis.pop()
        lis.pop()
        num1 = int(len(lis))
        num2 = int(num1/2)
        if lis[0:num2] == lis[num2: num1]:
            return int(len(lis))
assert find_even('abaababaab') == 6
1 个赞
def find_even(s: str):
    length = len(s)
    for i in range(1, length):
        if check_even(s[:-i]):
            return length - i
    else:
        return 0


def check_even(s: str):
    length = len(s)
    return s[:length // 2] == s[length // 2:]


assert find_even('abaababaab') == 6
def find_even(s: str):
    slist = list(s)
    while len(slist) != 0:
        slist.pop()
        slist.pop()
        length = len(slist)
        mid = int(length/2)
        if s[:mid] == s[mid: length]:
            return len(slist)
def find_even(s: str):
    str_len = int(len(s) / 2) - 1
    while str_len >= 1:
        if s[:str_len] == s[str_len:str_len * 2]:
            return str_len * 2
        str_len -= 1


assert find_even('abaababaab') == 6
def find_even(s:str):
    for i in range(int(len(s)/2)):
        s=s[:-2]
        if s[:int((len(s)/2))]==s[int(len(s)/2):]:
            return len(s)
def find_even(s: str):
    lis = list(s)
    while True:
        lis.pop()
        len1 = int(len(lis))
        len2 = int(len1/2)
        if lis[:len2] == lis[len2:len1]:
            return len(lis)

print(find_even('abaababaab'))
assert find_even('abaababaab') == 6
def find_even(s:str):
    sub = list(s[:-1])
    for i in range(len(sub)):
        size = len(sub)
        if size % 2 == 0: 
            if sub[:int(size/2)] == sub[int(size/2):]:
                return size
        sub.pop()

assert find_even('abaababaab') == 6

:partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face:

1 个赞

image

import math

def find_even(s: str):
    mid = int(math.floor(len(s)/2))
    for i in range(1,mid):
        if s[:(mid - i)] == s[(mid - i):2*(mid - i)]:
            return (2*len(s[:(mid - i)]))


assert find_even('abaababaab') == 6
def find_even(s: str):
    list_s = []
    for i in range(len(s)):
        if s[0:i]+s[0:i] == s[0:(2*i)]:
            list_s.append(s[0:(2*i)])
    return len(list_s[-2])


assert find_even('abaababaab') == 6