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

被折叠的 条评论
为什么被折叠?



