Python why is the string.index solution so fast?


  • 0
    H

    Hello everyone!

    I saw two python solution and I can't understand why solution 1 is so much faster than solution 2.
    Could someone enlighten me ?

    Solution 1:
    """

        self.f = True
        while len(s)>0:
            if s[0] in t:
                t = t[t.index(s[0])+1:]
                s = s[1:]
            else:
                return  False
                
        return self.f
    

    """

    Solution 2:
    """

        nt = len(t)
        ns = len(s)
        i = 0
        j = 0
        
        while True:
            if j == ns:
                return True
            if i == nt:
                return False
            
            if t[i] == s[j]:
                i += 1
                j += 1
            else:
                i += 1
    

    """


Log in to reply
 

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