One code template to solve all of these problems!
For 992. Subarrays with K Different Integers:\n\npython\nclass Solution:\n    def subarraysWithKDistinct(self, A: \'List[int]\', K: \'int\') -> \'int\':\n      
For [992. Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/):\n\n```python\nclass Solution:\n    def subarraysWithKDistinct(self, A: \'List[int]\', K: \'int\') -> \'int\':\n        return self.subarraysWithAtMostKDistinct(A, K) - self.subarraysWithAtMostKDistinct(A, K-1)\n    \n    def subarraysWithAtMostKDistinct(self, s, k):\n        lookup = collections.defaultdict(int)\n        l, r, counter, res = 0, 0, 0, 0\n        while r < len(s):\n            lookup[s[r]] += 1\n            if lookup[s[r]] == 1:\n                counter += 1\n            r += 1   \n            while l < r and counter > k:\n                lookup[s[l]] -= 1\n                if lookup[s[l]] == 0:\n                    counter -= 1\n                l += 1\n            res += r - l \n        return res\n```\n\nFor [3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/):\n\n```python\nclass Solution:\n    def lengthOfLongestSubstring(self, s):\n        """\n        :type s: str\n        :rtype: int\n        """\n        lookup = collections.defaultdict(int)\n        l, r, counter, res = 0, 0, 0, 0\n        while r < len(s):\n            lookup[s[r]] += 1\n            if lookup[s[r]] == 1:\n                counter += 1\n            r += 1\n            while l < r and counter < r - l:\n                lookup[s[l]] -= 1\n                if lookup[s[l]] == 0:\n                    counter -= 1\n                l += 1\n            res = max(res, r - l)\n        return res\n```\n\nFor [159. Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/):\n\n```python\nclass Solution(object):\n    def lengthOfLongestSubstringTwoDistinct(self, s):\n        """\n        :type s: str\n        :rtype: int\n        """\n        lookup = collections.defaultdict(int)\n        l, r, counter, res = 0, 0, 0, 0\n        while r < len(s):\n            lookup[s[r]] += 1\n            if lookup[s[r]] == 1:\n                counter += 1\n            r += 1   \n            while l < r and counter > 2:\n                lookup[s[l]] -= 1\n                if lookup[s[l]] == 0:\n                    counter -= 1\n                l += 1\n            res = max(res, r - l) \n        return res\n```\n\nFor [340. Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/):\n\n```python\nclass Solution(object):\n    def lengthOfLongestSubstringKDistinct(self, s, k):\n        """\n        :type s: str\n        :type k: int\n        :rtype: int\n        """\n        lookup = collections.defaultdict(int)\n        l, r, counter, res = 0, 0, 0, 0\n        while r < len(s):\n            lookup[s[r]] += 1\n            if lookup[s[r]] == 1:\n                counter += 1\n            r += 1   \n            while l < r and counter > k:\n                lookup[s[l]] -= 1\n                if lookup[s[l]] == 0:\n                    counter -= 1\n                l += 1\n            res = max(res, r - l) \n        return res\n```
[]