LeetCode:3. Longest Substring Without Repeating Characters

本文探讨了如何找出字符串中最长的无重复字符子串,并提供了三种不同的算法实现:暴力法、滑动窗口法及其优化版。通过这些方法,读者可以深入理解解决此类问题的关键技术和思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接

Question

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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

My Solution

Notice: There’re several typical cases you should pay attention to.

  1. Only one character exists.(Like ‘a’)
  2. No repeating character.(Like ‘abc’)

Train of thought:

I use two variables:temporal_result and final_result.

When a new character is found, it is appended to temporal_result. Check whether the length of final_result is smaller than that of temporal_result, and if so, final_result refers to temporal_result.

After processing all characters, we return the longer one between temporal_result and final_result in case of the last character being a totally new one.

class MySolution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        temporal_result = ''
        final_result = ''

        for character in s:
            if character not in temporal_result:
                temporal_result += character
            else:
                if len(final_result) < len(temporal_result):
                    final_result = temporal_result
                index = temporal_result.index(character)
                temporal_result = temporal_result[index+1:] + character

        if len(temporal_result) > len(final_result):
            return len(temporal_result)
        else:
            return len(final_result)

Editorial Solution of LeetCode

Approach One: Brute force [Time Limit Exceeded]

Train of thought:

Check every possible subtring, so it’s time complexity is rather high( O(n3) ).

Approach Two: Sliding Window

Train of thought:

Use HashSet to store the characters in current window [i, j)(j=i initially). Then slide the index j to the right. If it is not in the HashSet, slide j further. Doing so until s[j] is already in the HashSet. At this point, we found the maximum size of substrings without duplicate characters start with index i. If we do this for all i, we get our answer.

Approach Three: Sliding Window Optimized

Train of thought:

The approach has some commons with my solution. It updates the index of repeated character while mine keeps the initial index of the character when it is found, which means approach three tracks the latest longest substring and my solution gets the longest substring found earliest if there are at least two substrings with equal length (case like ‘abcda’, approach three gets ‘bcda’ and my method gets ‘abcd’).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值