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

被折叠的 条评论
为什么被折叠?



