Failing for last test case, output limit exceeded, 52 ms in python, need help


  • 0
    A
    class Solution(object):
        @staticmethod
        
        def getIndices(haystack,needle,l):
            for i in range(0,len(haystack)):
                if(haystack[i]==needle[0]):
                    l.append(i)
            
            return l
        
        def strStr(self, haystack, needle):
            """
            :type haystack: str
            :type needle: str
            :rtype: int
            """
            cnt=0;j=0;
            if(len(needle)==0):
                return 0
            if(len(haystack)==0):
                return -1
            if(len(needle)>len(haystack)):
                return -1
            l=[]
            l=Solution.getIndices(haystack,needle,l)
            if(l==[]):
                return -1
            print(l)
            for ind in l:
                print('checking with {}'.format(ind))
                for i in range(ind, len(needle)+ind):
                    if(len(haystack[ind:])<len(needle)):
                        return -1
                        
                    if(haystack[i]!=needle[j]):
                        print('verifyin {} with {}'.format(haystack[i],needle[j]))
                        j=0
                        cnt=0
                        break
                    else:
                        j+=1
                        cnt+=1
                        print('matching chars found {}'.format(haystack[i]))
                    
                    if(cnt==len(needle)):
                        print('found index {}'.format(i-2))
                        return (i-len(needle)+1)
            return -1            
            
    

Log in to reply
 

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