Python Codes with 6 lines


  • 2
    M
    class Solution(object):
    
        def lengthOfLongestSubstring(self, s):
            if not s:
                return 0
            T = [1] * len(s)
            for i in range(1, len(s)):
                T[i] = T[i - 1] - s[i - T[i - 1]:i].index(s[i]) if s[i] in s[i - T[i - 1]:i] else T[i - 1] + 1
            return max(T)
    

    I use an integer array T( len(T)= len(s)) to keep track of the length for the substring if the current character is just the end of the substring, and after one loop, the max number in the array is the answer we want.

    b p f b h m i p x
    1
    1 2
    1 2 3
    1 2 3 3
    for the second b, it should be 4 if there's no another b in the previous string, but it turns out there already exists b in the seq(bpf), and since b is in the position 1 of seq(bpf), the value for second b is 4 - 1 = 3, and make up a new seq(pfb) with length = 3

    b p f b h m i p x
    1 2 3 3 4 5 6
    1 2 3 3 4 5 6 6
    1 2 3 3 4 5 6 6 7
    for the second p, it should be 7 if there's no another p in the previous string(pfbhmi), note that the previous seq is 'pfbhmi' due to the value for the previous characher 'i' is 6, and len('pfbhmi') = 6, but there's another 'p' in the previous seq, and the position for the 'p' is 1 in seq(pfbhmi), so the value for the second p is 7 - 1 = 6, which make it another seq(fbhmip) with length 6;


Log in to reply
 

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