[LeetCode] 3. The longest substring without repeated characters. Difficulty Level: Moderate.

[LeetCode] 3. The longest substring without repeated characters. Difficulty Level: Moderate.

1. Topic

insert image description here

2. My answer: violent cycle, O(n 2 )

Violent double for loop, time complexity O(n 2 )

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if s=="":
            return 0
        length=1
        for i in range(0,len(s)-1):
            subStr=s[i]
            for j in range(i+1,len(s)):
                if s[j] not in subStr:
                    subStr+=s[j] 
                    length=max(length,len(subStr))
                else:
                    break
        return length

No better way has been found so far.

Guess you like

Origin blog.csdn.net/qq_43799400/article/details/130875086