【每日一题20220629】合并字符串检查器

:mage:‍进入魔法世界之前,您要面临一个挑战,编写一个算法来检查给定的字符串 , 是否s 可以由其他两个字符串part1part2 组成。

限制是part1 和中的字符应与part2 中的字符顺序相同s

魔法师给你下面的例子。

例如:

'hogwarts' 由 'hgr' and 'owats' 合并:

    s:  h o g w a r t s   = hogwarts
part1:  h   g     r     = hgr
part2:    o   w a   t s   = owats

题目难度:中等

题目来源: https://www.codewars.com/kata/54c9fcad28ec4c6e680011aa

def is_merge(s: str, part1: str, part2: str) -> bool:
    """your code"""


assert is_merge("hogwarts","hgr","owats") == True
assert is_merge("hogwarts","hog","warts") == True
assert is_merge("hogwarts","hgr","wats") == False
def is_merge(s: str, part1: str, part2: str) -> bool:
    """your code"""
    str2 = part1+part2
    if len(s) == len(str2):
        ls =[i for i in s]
        for i in str2:
            if i in ls:
                ls.remove(i)
        if len(ls) == 0:
            return True
        else:
            return False
    else:
        return False

if __name__ == '__main__':
    assert is_merge("hogwarts","hgr","owats") == True
    assert is_merge("hogwarts","hog","warts") == True
    assert is_merge("hogwarts","hgr","wats") == False
    assert is_merge("hogwarts", "hgr", "wssaaats") == False
    assert is_merge("hogwarts", "hgr", "waoas") == False
# daily_0415
def is_merge(s: str, part1: str, part2: str) -> bool:
    if len(s) <= 1:
        return s == part1 + part2
    elif len(part1) > 0 and len(part2) > 0 and s[0] == part1[0] and s[0] == part2[0]:
        return is_merge(s[1:], part1[1:], part2) or is_merge(s[1:], part1, part2[1:])
    elif len(part1) > 0 and s[0] == part1[0]:
        return is_merge(s[1:], part1[1:], part2)
    elif len(part2) > 0 and s[0] == part2[0]:
        return is_merge(s[1:], part1, part2[1:])
    return False
def is_merge(s: str, part1: str, part2: str) -> bool:
    for i,v in enumerate(s):
        if len(part1) != 0 and ord(v)==ord(part1[0]):
            part1 = part1[1:]
            continue
        if len(part2) != 0 and ord(v)==ord(part2[0]):
            part2 = part2[1:]
            continue
        return False
    return True


assert is_merge("hogwarts","hgr","owats") == True
assert is_merge("hogwarts","hog","warts") == True
assert is_merge("hogwarts","hgr","wats") == False
def is_merge(s: str, part1: str, part2: str) -> bool:
    #递归
    if not part1:
        return s == part2
    if not part2:
        return s == part1
    if not s:
        return part1+part2 == ''
    if s[0]==part1[0] and is_merge(s[1:],part1[1:],part2):
        return True
    if s[0]==part2[0] and is_merge(s[1:],part1,part2[1:]):
        return True
    return False

assert is_merge("hogwarts","hgr","owats") == True
assert is_merge("hogwarts","hog","warts") == True
assert is_merge("hogwarts","hgr","wats") == False
def is_merge(s: str, part1: str, part2: str) -> bool:
    """your code"""
    if len(s) != len(part1) + len(part2):
        return False
    if (len(part1) == 1 and s == part1) or (len(part2) == 1 and s == part2):
        return True
    if len(part1) > 0 and len(part2) > 0 and s[0] == part1[0] and s[0] == part2[0]:
        return is_merge(s[1:], part1[1:], part2) or is_merge(s[1:], part1, part2[1:])
    if len(part1) > 0 and s[0] == part1[0]:
        return is_merge(s[1:], part1[1:], part2)
    if len(part2) > 0 and s[0] == part2[0]:
        return is_merge(s[1:], part2[1:], part1)


assert is_merge("hogwarts", "hgr", "owats") == True
assert is_merge("hogwarts", "hog", "warts") == True
assert is_merge("hogwarts", "hgr", "wats") == False
assert is_merge("hhgwaatss", "hgwas", "hats") == True
def is_merge(s: str, part1: str, part2: str) -> bool:
	s=list(s)
	if len(part1+part2)==len(s):
		for i in part1:
			s.remove(i)
		return True if "".join(s)==part2 else False

	else:
		return False
assert is_merge("hogwarts","hgr","owats") == True
assert is_merge("hogwarts","hog","warts") == True
assert is_merge("hogwarts","hgr","wats") == False

应该有点问题,如果part1中的字符顺序和s中的不一致,也会返回True;
比如:is_merge(“hogwarts”,“grh”,“owats”)

建议加上长度的判断,比如assert is_merge(“hogwarts”,“hog”,“wartsas”)是不成立的

def is_merge_2(s: str, part1: str, part2: str) -> bool:
    """solution #2 deal with ordering of part1 and part2 strings"""
    if len(part1 + part2) == len(s):
        index1, index2 = 0, 0
        for i in range(len(s)):
            if index1 < len(part1) and s[i] == part1[index1]:
                index1 += 1
            elif index2 < len(part2) and s[i] == part2[index2]:
                index2 += 1
            else:
                return False
        return True
    else:
        return False


def main():
    assert is_merge_2("hogwarts", "hgr", "owats") == True
    assert is_merge_2("hogwarts", "hog", "warts") == True
    assert is_merge_2("hogwarts", "hgr", "wats") == False
    assert is_merge_2("hogwarts", "hgrt", "woas") == False


if __name__ == "__main__":
    main()
def is_merge(s: str, part1: str, part2: str) -> bool:
    if len(s)!=len(part1)+len(part2):
        return False
    else:
        index_list1=[s.index(part1[i]) if part1[i] in s else 'x' for i in range(len(part1))]
        index_list2=[s.index(part2[i]) if part2[i] in s else 'x' for i in range(len(part2))]
        if 'x' not in index_list1 and 'x' not in index_list2:
            if sorted(index_list1)==index_list1 and sorted(index_list2)==index_list2:
                index_res_list=sorted(index_list1+index_list2)
                if index_res_list == [i for i in range(len(s))]:
                    return True
                else:
                    return False
            else:
                return False
        else:
            return False

assert is_merge("hogwarts","hgr","owats") == True
assert is_merge("hogwarts","hog","warts") == True
assert is_merge("hogwarts","hgr","wats") == False
assert is_merge("hogwarts","hgo","warts") == False
assert is_merge("hogwarts","hog","awrts") == False