Merged String Checker 5kyu

该问题要求编写一个算法来检查给定的字符串s能否由part1和part2按原顺序合并而成,且字符不重复。提供的解决方案是合并part1和part2,然后对比与s中每个字符出现的次数,若所有字符都只出现两次则返回True,否则返回False。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

At a job interview, you are challenged to write an algorithm to check if a given string, s, can be formed from two other strings, part1 and part2.

The restriction is that the characters in part1 and part2 should be in the same order as in s.

The interviewer gives you the following example and tells you to figure out the rest from the given test cases.

For example:

'codewars' is a merge from 'cdw' and 'oears':

    s:  c o d e w a r s   = codewars
part1:  c   d   w         = cdw
part2:    o   e   a r s   = oears

根据问题描述,主要是判断part1+part2 是否可以组成 S,字符串不能重复出现。

我的解题思路:

可以把part1和part2合并,与s去比较字符出现次数,如果都是2那就是一致。

上代码:

def is_merge(s, part1, part2):
    text=s+''.join(part1)+''.join(part2)
    for i in text:
        num=text.count(i)
        if num==1:
            return False
    return True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值