查看: 1899| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[字符串] Sliding window 理解与运用【英文版】

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
从一道题目引申出hashtable的用法,由于当时写心得的时候用的是英文, 然后我现在也没有时间去翻译, 但是我觉得总结挺不错的, 希望你们可以喜欢。

Longest Substring Without Repeating Characters - https://leetcode.com/problems/lo ... peating-characters/


Solution:
1. keep a hashtable; key represent character in the string, and val represents the index of that character in that string
2. iterate through the string, and those character to the hashtable
3. when we encounter a existing character in the hashtable, that means we will need to move the left pointer, so that the current character is always

Template:
    def lengthOfLongestSubstring(self, s):
        hashtable = {}
        left = 0
        right = 0
        res = ''
        for i, char in enumerate(s):
            if char in hashtable:
                new_left = hashtable[char] + 1
                while left < new_left:
                    del hashtable[s[left]]
                    left += 1
            hashtable[char] = i
            res = s[left:i+1] if len(res) < (i+1-left) else res
        return len(res)

Tips:
1. Sliding window question is for question that requires substring. We can put this substring into a sliding window, and have a hashtable that keep tracks of the elements in that sliding window.
2. For sliding window, the question doesn’t require order on the character, that’s why we can just use hashtable as a way to find the right substring. Otherwise, we need to use the LCS (Longest Common Subsequence) to handle those string questions.
3. The core concept of sliding window is that we always keep the current character in that sliding window.  To keep the current character, it might not meet the condition, that’s why we need to move the left pointer to shrink the window to make it meet the conditions.
4. So now the question went from how to find the substring from that condition to how to find the substring and hashtable that satisfy the condition
5. How to find the condition in terms of the hashtable?
    1. Longest substring without repeated character => every time we encounter an existing character, we move the left pointer to hashtable[character] + 1, so now the sling window is valid for the conditions. During the movement/sliding, we also need to clear out some elements in the hashtable since now they are not in sliding window anymore. So the condition becomes ==> character not in hashtable
    2. Longest Repeating character replacement => Notice that this is a different type sliding window question. The above question is about finding the string, and that requires index of that string, but this question doesn’t This question is about finding the longest substring and it’s related to its character counts. So for this sliding window question, we need to keep track of the current sliding window character count. For this question, the condition in terms of the hashtable, we need to make sure the max_count  character in that hashtable has the following relationship: i + 1 - left - max_count <= k;  When our window doesn’t satisfy this condition, we shrink it to make it satisfy this condition. During this process, we can also keep track of the final answer.
    3. Longest Substring with at most K distinct character => This requires us to find the substring and it’s associated with the character count => (sliding window). At most K distinct character ==> len(hashtable) <= k.
    4. Minimum Window Substring => This requires us to find the minimum substring that contains all the characters from the other string. This could essentially be LCS if it requires order/sequence on the characters. The condition for this => count = len(hashtable), have a count that keep track of how many characters we need to find for string2, and then we try to keep the count == 0
    5. Permutation in Strings => This requires use to find the exact length of substring in string1 that contains all the characters from string2, very similar to minimum window substring, but the string length is fixed. So we just need to make sure that sliding_window_hashtable matches the sliding window for string2

评分

参与人数 3大米 +7 收起 理由
feifei0333 + 1 给你点个赞!
Husky_wang + 3 给你点个赞!
14417335 + 3

查看全部评分


上一篇:刷题心得
下一篇:立个flag,中秋三天我要刷题100+
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表