Simple two dict solution in python


  • 0
    W
    class Solution(object):
        def findSubstring(self, s, words):
            vis = {}
            for w in words:
                vis[w] = vis.get(w, 0) + 1
            
            wl = len(words[0])
            wnum = len(words)
            ll = wl * wnum
            sl = len(s)
            i = 0
            res = []
            while i <= sl - ll:
                bgn = i
                end = i + ll
                used = {} 
                while i < end:
                    tmp = s[i:i+wl]
                    if not tmp in vis:
                        break
                    used[tmp] = used.get(tmp, 0) + 1
                    if used[tmp] > vis[tmp]:
                        break
                    i += wl
                if i == end:
                    res.append(bgn)
                i = bgn + 1
            return res

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.