将A中的字母与出现次数储存,与B进行对比,一旦B中的字母不在A中返回false,如果在,将字母出现次数-1,最后查找是否有小于0的情况。
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
if len(B)==0:
return True
if len(A)==0:
return False
mydict=dict()
for i in A:
if i not in mydict.keys():
mydict[i]=1
else:
mydict[i]+=1
for i in B:
if i not in mydict.keys():
return False
else:
mydict[i]-=1
for i in mydict.values():
if i<0:
return False
return True