Merged String Checker Codewars 答题

在一次面试中,你被要求编写一个算法,检查字符串s是否能由part1和part2按顺序拼接而成。例如,'codewars'可以由'cdw'和'oears'按顺序组成。你需要编写一个函数,根据输入的s, part1, part2判断条件是否满足。你首先考虑使用正则表达式来解决这个问题。" 116030434,10544612,matplotlib.pyplot.plot详解与示例,"['Python', '数据可视化', 'matplotlib', '绘图']
摘要由CSDN通过智能技术生成

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

字符串融合,编写一个函数,给定三个参数,s,part1,part2
判断s是不是有part1和part2组合而成的,并且part1和part2的字母顺序应该和S里的字母顺序一致。
函数嵌套可以解决这个问题,但是我第一时间想到的是正则匹配。

import re

def is_merge(s, part1, part2):
    if s == part1 + part2:
        return True
    if len(part1)+len(part2) != len(s) or sorted(list(s)) != sorted(list(part1+part2)):
        return False
    li
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值