【每日一题20220812】重组乱码

如果部分字符可以重新排列后匹配想要的结果,则完成scramble(str1, str2) 返回True,否则返回False。

笔记:

  • 仅使用小写字母 (az)。不包含标点符号或数字。
  • 需要考虑性能。

例子

scramble('rkqodlw', 'world') ==> True
scramble('cedewaraaossoqqyt', 'codewars') ==> True
scramble('katas', 'steak') ==> False

题目难度:一般
题目来源:https://www.codewars.com/kata/55c04b4cc56a697bb0000048

def scramble(s1: str, s2: str)  -> bool:
    # your code here

assert scramble('rkqodlw', 'world') ==  True
assert scramble('cedewaraaossoqqyt', 'codewars') == True
assert scramble('katas', 'steak') == False
assert scramble('scriptjava', 'javascript') == True
assert scramble('scriptingjava', 'javascript') == True
assert scramble('scriptingjav', 'javascript') == False
def scramble(s1: str, s2: str)  -> bool:
	list1=list(s2)
	for i in range(len(list1)):
		if list1[i] not in s1:
			return False
		if list1[i] in s1 and s1.count(list1[i])<s2.count(list1[i]):
			return  False
	return True


assert scramble('rkqodlw', 'world') ==  True
assert scramble('cedewaraaossoqqyt', 'codewars') == True
assert scramble('katas', 'steak') == False
assert scramble('scriptjava', 'javascript') == True
assert scramble('scriptingjava', 'javascript') == True
assert scramble('scriptingjav', 'javascript') == False

image

1 个赞
def scramble(s1: str, s2: str)  -> bool:
    # your code here
    for s in set(s2):
        if s1.count(s) < s2.count(s):
            return False
    return True


assert scramble('rkqodlw', 'world') ==  True
assert scramble('cedewaraaossoqqyt', 'codewars') == True
assert scramble('katas', 'steak') == False
assert scramble('scriptjava', 'javascript') == True
assert scramble('scriptingjava', 'javascript') == True
assert scramble('scriptingjav', 'javascript') == False
def scramble(s1: str, s2: str) -> bool:
    res_list = []
    for i in range(len(s2)):
        if s2[i] in s1:
            s1 = s1.replace(s2[i], '', 1)
            res_list.append(s2[i])
    if ''.join(res_list) == s2:
        return True
    else:
        return False


assert scramble('rkqodlw', 'world') == True
assert scramble('cedewaraaossoqqyt', 'codewars') == True
assert scramble('katas', 'steak') == False
assert scramble('scriptjava', 'javascript') == True
assert scramble('scriptingjava', 'javascript') == True
assert scramble('scriptingjav', 'javascript') == False