1.问题描述:
比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母
2.样例:
给出 A = "ABCD" B = "ACD",返回 true
给出 A = "ABCD" B = "AABC", 返回 false
3.代码:
class Solution:
"""
@param A: A string
@param B: A string
@return: if string A contains all of the characters in B return true else return false
"""
def compareStrings(self, A, B):
# write your code here
dict_a = {}
dict_b = {}
if B == "":
return True
if A == "":
return False
for i in A:
if i not in dict_a:
dict_a[i] = 1
else:
dict_a[i] += 1
for i in B:
if i not in dict_b:
dict_b[i] = 1
else:
dict_b[i] += 1
for k, v in dict_b.items():
if k not in dict_a.keys() or v > dict_a[k]:
return False
return True
先将A和B中的字母计数存到各自的字典里,然后利用字典遍历的key和value值来判断。