[Power button - small daily practice] 3. No repeating characters longest substring (python)

3. No repeating characters longest substring

Topic links: https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

Title Description

Given a string, you find out which does not contain a repeated character longest substring length.

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: Because the longest substring of characters without repetition is "abc", so its length is 3.

Example 2:

Input: "BBBBB"
Output: 1
Explanation: Because the longest substring is repeated characters without "b", so that its length is 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: Because the longest sub-string is repeated characters without "wke", so its length is 3.
cautionYour answer must be a substring of length, "pwke" is a sub-sequence, not a substring.

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        n = len(s)
        sub_str = ''
        temp = []
        for i in range(n):
            for j in range(i,n):
                char = s[j]
                if char in temp:
                    j += 1
                    break
                else:
                    temp.append(char)

            if len(temp) > len(sub_str):
                sub_str = ''.join(temp)
            temp = []

        return len(sub_str)
Published 44 original articles · won praise 5 · Views 4471

Guess you like

Origin blog.csdn.net/ljb0077/article/details/104705989