Can somebody tell me why I can not pass the last test case?


  • 0
    H
    class Solution(object):
        def findAnagrams(self,s, p):
            """
            :type s: str
            :type p: str
            :rtype: List[int]
            """
            begin = 0
            end = 0
            dic = {}
            res = []
            count = len(p)
            for element in p:
                if element not in dic:
                    dic[element] = 1
                else:
                    dic[element] += 1
            while end < len(s):
                while end - begin == len(p):
                    if s[begin] in dic and dic[s[begin]] == 0:
                        dic[s[begin]] += 1
                        count += 1
                    elif s[begin] in dic and dic[s[begin]] < 0:
                        dic[s[begin]] += 1
                    begin += 1
                if s[end] in dic and dic[s[end]] > 0:
                    dic[s[end]] -= 1
                    count -= 1
                elif s[end] in dic and dic[s[end]] <= 0:
                    dic[s[end]] -= 1
                if count == 0:
                    res.append(begin)
                end += 1
            return res
    

    There is something wrong with the last test case, can somebody help me out?
    How can I revise my code?


Log in to reply
 

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