Substring with Concatenation of All Words

来源:互联网 发布:百锐腾软件 编辑:程序博客网 时间:2024/06/16 02:06

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.

For example, given:
S"barfoothefoobarman"
L["foo", "bar"]

You should return the indices: [0,9].

(order does not matter).

class Solution:    # @param S, a string    # @param L, a list of string    # @return a list of integer    def findSubstring(self, S, L):        ret=[]        step,lenL,lenS = len(L[0]),len(L),len(S)        i=0        while i+step*lenL-1<lenS:            flag=True            listS=[S[k : k + step] for k in xrange(i, i + lenL * step, step)]            for item in L:                if  item  in  listS:                    listS.remove(item)                else:                    flag=False                    break            if flag:                ret.append(i)            i=i+1        return ret


0 0