我一直在Longest Substring Without Repeating Characters - LeetCode上花费时间
- 最长的不重复字符的子字符串
中等
给出一个字符串,找到最长子字符串的长度,而不重复字符。
示例1:
Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.
示例2:
Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.
示例3:
Input: "pwwkew" Output: 3 Explanation: 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.
可以使用两个指针混合的kadane算法处理子数组来解决该问题
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
logging.debug(f"{list(enumerate(s))}")
slo = fas = 0 #slow as the fisrt character in a subarray which not duplicate, fast as the fast.
#relation: length = fas - slo
current = set()
glo = loc = 0
while fas < len(s):
logging.debug(f"pre_current: {current}, slow: {slo}, fast: {fas}")
if s[fas] not in current:
current.add(s[fas]
loc = fas - slo
glo = max(glo, loc)
fas +=1
else:
current.remove(s[slo])
slo += 1
logging.debug(f"post_current: {current}, slow: {slo}, fast: {fas} \n")
return glo
TestCase
def test_g(self):
s = "abccefg"
answer = 4
check = self.solution.lengthOfLongestSubstring(s)
self.assertEqual(answer, check)
解决方案非常明显,可以交替缓慢和快速移动
$ python 3.LongestSubstring.py MyCase.test_g
DEBUG [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'c'), (4, 'e'), (5, 'f'), (6, 'g')]
DEBUG pre_current: set(), slow: 0, fast: 0
DEBUG post_current: {'a'}, slow: 0, fast: 1
DEBUG pre_current: {'a'}, slow: 0, fast: 1
DEBUG post_current: {'b', 'a'}, slow: 0, fast: 2
DEBUG pre_current: {'b', 'a'}, slow: 0, fast: 2
DEBUG post_current: {'b', 'c', 'a'}, slow: 0, fast: 3
DEBUG pre_current: {'b', 'c', 'a'}, slow: 0, fast: 3
DEBUG post_current: {'b', 'c'}, slow: 1, fast: 3
DEBUG pre_current: {'b', 'c'}, slow: 1, fast: 3
DEBUG post_current: {'c'}, slow: 2, fast: 3
DEBUG pre_current: {'c'}, slow: 2, fast: 3
DEBUG post_current: set(), slow: 3, fast: 3
DEBUG pre_current: set(), slow: 3, fast: 3
DEBUG post_current: {'c'}, slow: 3, fast: 4
DEBUG pre_current: {'c'}, slow: 3, fast: 4
DEBUG post_current: {'c', 'e'}, slow: 3, fast: 5
DEBUG pre_current: {'c', 'e'}, slow: 3, fast: 5
DEBUG post_current: {'e', 'f', 'c'}, slow: 3, fast: 6
DEBUG pre_current: {'e', 'f', 'c'}, slow: 3, fast: 6
DEBUG post_current: {'g', 'e', 'f', 'c'}, slow: 3, fast: 7
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
最后,该解决方案采用了两种指针技术以及Kadane算法的思想。我认为,花了数小时的时间对初学者进行调试后,最终可以解决问题。
但是,我读到了一个如此精致的解决方案
class SolutionA:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
#slow is the first which not duplicate in a subarray
#fast is the last whichi not duplicate in a subarray
lookup, glo, slo, fas = {}, 0, 0, 0
for fas, ch in enumerate(s):
if ch in lookup:
slo = max(slo, lookup[ch]+1)
elif ch not in lookup:
glo = max(glo, fas-slo+1)
lookup[ch] = fas #update the duplicates and add new
return glo
该解决方案非常聪明,老实说,如果以前没有阅读过,我不相信有人会在几个小时内设计出这样的解决方案。
它使用了哈希图,这是kadane的算法思想的两倍,结构非常简洁。
作为两个指针是一种常见的技术吗?它叫什么名字
答案 0 :(得分:1)
如第二种方法的解决方案注释中所述:
慢是第一个在子数组中不重复的
fast是最后一个在子数组中没有重复的
它使用2个指针来跟踪没有重复字符的窗口大小。如果找到重复项,它将相应地更新指针。
换句话说,它维护一个窗口并将其进一步滑动以查看使用non-repeating characters
属性可以持续多长时间。因此,此方法称为sliding window technique。
这对于只有26个字母字符的字符串来说似乎微不足道,但是对于UTF-8类型的字符串来说非常有用。