Leetcode Q3

the original question is:

Given a string s, find the length of the longest substring without duplicate characters.

Consider those example, 

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3. Note that"bca" and "cab" are also correct answers.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

My first code is 

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        a = []#this is the list of string
        b = []#this is list for every element's biggest substring
        for i in s:
            a.append(i)
            b.append(0)
        if len(a) == 1:
            b[0] = 1
        else:
            for i in range(len(a)):
                for j in range(len(a)-i-1):
                    m = 0 
                    for k in range(j+1):
                        if a[i+j-k] != a[j+1+i]:
                            m = m+1
                    if m  == j+1:
                        b[i] = m + 1
                    elif m == 0:
                        b[i] = 1
                        break
                    else:
                        break

        return max(b) if b else 0

This answer seems like good , but it truly time-consuming. Let me explain my thinking pattern:

We firstly transit the string to a list, and the index help to search every elements quickly, and we judge the string is single char or not. For single one, we return 1, for another one, we come in the loop. 

I loop i the every element to calculate the longest substring respectively. For every element, we loop j again the all the elements which behind this one. For j, we  should compare with each one between the i and j. If the element is equivalent, m++. After we compare those one, if number m is equivalent to the number between i and j (m=j-i). We remeber this m by b[i] = m+1(Note: m = j-i, and length of the string is m+1). Continue to loop, if I find someone can not satisfy the condition (a[i+j-k] != a[j+1+i]), we just break the loop. What is more, the last loop already remeber the b[i], so don worried about if break the loop, it would miss the remebering the value.

Finally, return max(b) if b else 0. It just make sure we could deal with the empty string. 


But, sorry. the code exceed the time limit. If for a very very long string, we should compute efficiently. For example:

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        a = []  # list of characters
        b = []  # list to store each starting position’s longest substring length
        for i in s:
            a.append(i)
            b.append(0)

        for i in range(len(a)):
            seen = set()  # 用来存放当前子串出现的字符
            m = 0          # 当前子串长度

            for j in range(i, len(a)):   # 从第 i 个字符开始往右扩展
                if a[j] not in seen:
                    seen.add(a[j])
                    m += 1
                else:
                    break  # 一旦遇到重复字符,跳出循环
            b[i] = m        # 记录从 i 出发的最长子串长度
        return max(b) if b else 0 

Explanation:

we loop every elements, and we set a set() which help me to storage the substring. We loop the j after i and we put it into the set() when we find someone which is not included in the set(). and m++(or just len(a)). If find one in this set(), remeber the m and break this loop to calculate next loop. The core is "not in" and "a[i+j-k] != a[j+1+i]". The latter one is not time-consuming, the last one need to compare many many times

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值