【每日一题20220415】文字合成

def solution(s: str, part1: str, part2: str)-> bool:
    # your code here
    if len(s) != len(part1) + len(part2):
        return False
    tmp = ''
    a = 0
    for i in range(len(s)):
        if len(part1) > 0 and len(part2) > 0:
            try:
                if s[i] == part1[a] and s[i] == part2[a]:
                    tmp += s[i] 
                    a += 1
            except:
                if len(part1) > len(part2)
                    part2 = part2.replace(tmp, '', 1)
                    tmp = ''
                    a = 0
                    if len(part1) > 0 and s[i] == part1[a]:
                        part1 = part1.replace(s[i], '', 1)
                        tmp = ''
                        a = 0
                    else:
                        return False
                else:
                    part1 = part1.replace(tmp, '', 1)
                    tmp = ''
                    a = 0
                    if len(part2) > 0 and s[i] == part2[a]:
                        part2 = part2.replace(s[i], '', 1)
                        tmp = ''
                        a = 0
                    else:
                        return False
        elif len(part1) > 0 and s[i] == part1[a]:
            part1 = part1.replace(tmp + s[i], '', 1)
            tmp = ''
            a = 0
        elif len(part2) > 0 and s[i] == part2[a]:
            part2 = part2.replace(tmp + s[i], '', 1)
            tmp = ''
            a = 0
        else:
            return False
    else:
        return True
        

assert solution('hogwarts','hws','ogart') is True
assert solution('codewars', 'code', 'wars') is True
assert solution('codewars', 'cod', 'wars') is False
assert solution('sssyyysss', 'ssyys', 'syss') is True
assert solution('hwhharts', 'hhs', 'hwart') is True
assert solution('ababab', 'bab', 'aba') is True
assert solution('haah', 'ha', 'ha') is False

def solution(s: str, part1: str, part2: str)-> bool:
    # your code here
    li_part1 = list(part1)
    li_part2 = list(part2)
    if len(s) != len(part1+part2):
        return False
    else:
        for i in s:
            if len(li_part1)>0 and i == li_part1[0]:
                li_part1.pop(0)
            elif len(li_part2)>0 and i == li_part2[0]:
                li_part2.pop(0)
            else:
                return False
        return True

assert solution('hogwarts','hws','ogart') is True
assert solution('codewars', 'code', 'wars') is True
assert solution('codewars', 'cod', 'wars') is False
assert solution('codewars', 'code', 'warsl') is False
def solution(s: str, part1: str, part2: str)-> bool:
    # your code here
    if len(part1) + len(part2) != len(s):
        return False

    for data in s:
        if data in part1:
            part1 = part1[part1.index(data)+1:]
            print(f"match part1:{data},part1 left:{part1}")
            continue
        if data in part2:
            part2 = part2[part2.index(data)+1:]
            print(f"match part2:{data},part2 left:{part2}")
            continue
        return False

    return True


assert solution('hogwarts','hws','ogart') is True
assert solution('codewars', 'code', 'wars') is True
assert solution('codewars', 'cod', 'wars') is False
assert solution('ababab', 'bab', 'aba') is True
assert solution('haah', 'ha', 'ha') is False
def solution(s: str, part1: str, part2: str)-> bool:
    res_list=[]
    for i in range(len(s)):
        if s[i] in part1 or s[i] in part2:
            res_list.append(s[i])
    for j in range(1,len(part1)):
        if s.index(part1[j])<s.index(part1[j-1]):
            return False
    for k in range(1,len(part2)):
        if s.index(part2[k])<s.index(part2[k-1]):
            return False
    str_res=''.join(res_list)
    if str_res==s:
        return True
    else:
        return False

assert solution('hogwarts','hws','ogart') is True
assert solution('codewars', 'code', 'wars') is True
assert solution('codewars', 'cod', 'wars') is False
assert solution('codewars', 'cdoe', 'wars') is False