Skip to main content

Leetcode 424. Longest Repeating Character Replacement. Python (Sliding Window)

 424. Longest Repeating Character Replacement


You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.

Return the length of the longest substring containing the same letter you can get after performing the above operations.

 

Example 1:

Input: s = "ABAB", k = 2
Output: 4
Explanation: Replace the two 'A's with two 'B's or vice versa.

Example 2:

Input: s = "AABABBA", k = 1
Output: 4
Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of only uppercase English letters.
  • 0 <= k <= s.length


Solution : 

class Solution:
    def characterReplacement(self, s: str, k: int) -> int:
        hm = {}
        ans = 0
        
        l = 0
        for r in range(len(s)):
            hm[s[r]] = 1 + hm.get(s[r],0)
            
            while (r-l+1) - max(hm.values()) > k:
                hm[s[l]] -= 1
                l += 1
                
            ans = max(ans, r-l+1)
        return ans


Explaination :






Comments

Popular posts from this blog

May-6 2020 Challenge

  6.   Majority Element Given an array of size  n , find the majority element. The majority element is the element that appears  more than   ⌊ n/2 ⌋  times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: Input: [3,2,3] Output: 3 Example 2: Input: [2,2,1,1,1,2,2] Output: 2 Solution in Java  class Solution {     public int majorityElement(int[] num) {         int m = num[0], cnt= 1;     for (int i = 1; i < num.length; i++) {         if (cnt == 0) {             m= num[i];             cnt = 1;         } else if (num[i] == m) {             cnt++;         } else              cnt--;    }      return m;...

Leetcode 190. Reverse Bits. Python. Bit Manipulation

  190 .  Reverse Bits Reverse bits of a given 32 bits unsigned integer. Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using  2's complement notation . Therefore, in  Example 2  above, the input represents the signed integer  -3  and the output represents the signed integer  -1073741825 .   Example 1: Input: n = 00000010100101000001111010011100 Output: 964176192 (00111001011110000010100101000000) Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000 . Example 2: Input: n = 11111111111111111111...

Leetcode 58. Meeting Rooms II. Python (Easiest Explaination)

  58.  Meeting Rooms II Description Given an array of meeting time intervals consisting of start and end times  [[s1,e1],[s2,e2],...] (si < ei) , find the minimum number of conference rooms required.) (0,8),(8,10) is not conflict at 8 Example Example1 Input : intervals = [(0,30),(5,10),(15,20)] Output : 2 Explanation : We need two meeting rooms room1 : ( 0 , 30 ) room2 : ( 5 , 10 ),( 15 , 20 ) Example2 Input: intervals = [( 2 , 7 )] Output: 1 Explanation: Only need one meeting room Solution : """ Definition of Interval: class Interval(object):     def __init__(self, start, end):         self.start = start         self.end = end """ class Solution:     """     @param intervals: an array of meeting time intervals     @return: the minimum number of conference rooms required     """     def min_meeting_rooms( self , intervals: List[Interval]) -> int :   ...