255. Multi-string search

255. Multi-string search

中文English

给出一个源字符串sourceString和一个目标字符串数组targetStrings,判断目标字符串数组中的每一个字符串是否是源字符串的子串

样例

样例 1:

输入: sourceString = "abc" ,targetStrings = ["ab","cd"]
输出: [true, false]

样例 2:

输入: sourceString = "lintcode" ,targetStrings = ["lint","code","codes"]
输出: [true,true,false]	

注意事项

len(sourceString) <= 1000
sum(len(targetStrings[i])) <= 1000

 
 
输入测试数据 (每行一个参数)如何理解测试数据?
class Solution:
    """
    @param sourceString: a string
    @param targetStrings: a string array
    @return: Returns a bool array indicating whether each string in targetStrings is a substring of the sourceString
    """
    def whetherStringsAreSubstrings(self, sourceString, targetStrings):
        # write your code here
        #子串,连续的字符串
        
        res = []
        length = len(sourceString)
        
        #主方法
        for target in targetStrings:
            if len(target) > length:
                res.append(False)
                continue
            
            #判断
            flag = False 
            l = len(target)
            for j in range(length - l + 1):
                if sourceString[j: j + l] == target:
                    flag = True
                    break
                
            res.append(flag)
        
        return res
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/13283668.html