1.长度为m的字符串a,长度为n的字符串b,(m>n) 判断b中的字母是否全在a中? O(n)最小. class Num(object): def getNum(self, m): numList = filter(lambda x: not [x%i for i in range(2,x) if x%i==0], range(2,500)) return numList[0:m] def getSizeDict(self, string): numList = self.getNum(len(string)) strDict = {} for s in set(string): strDict[s] = numList[0] del numList[0] return strDict def getDiff(self, string, string2): j = 1 flag = 1 strDict = self.getSizeDict(string) numList =self.getNum(len(string)) for i in numList: j*=i for s in string2: try: d = strDict[s] except: flag = 0 break return flag if __name__ == ‘__main__‘: string = raw_input(‘string:‘) string2 = raw_input(‘another:‘) main = Num() num = main.getDiff(string,string2) print num == 1 and ‘yes‘ or ‘no‘
python小算法
时间: 2024-10-09 03:51:53