1208. Get Equal Substrings Within Budget

알고리즘 문제풀기 · 2020. 1. 29. 20:41

https://leetcode.com/problems/get-equal-substrings-within-budget/

 

Get Equal Substrings Within Budget - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

슬라이딩 윈도우와 큐를 생각해봐야하는 문제.

 

ord를 써서 아스키 값으로 변환하고

 

가장 긴 길이의 substring값을 업데이트 해주는 형식

 

 

class Solution(object):
    def equalSubstring(self, s, t, maxCost):
        """
        :type s: str
        :type t: str
        :type maxCost: int
        :rtype: int
        """
        
        
        
        diff    = []
        max_sum = 0
        
        max_length = 0
        
        for i in range(0, len(s), 1):
            
            
            diff_value = abs(ord(s[i])-ord(t[i])) 
            diff.append(diff_value)
            
            max_sum += diff_value
            #print(diff)
            while maxCost < max_sum:
                
                value = diff.pop(0)
                
                max_sum -= value
        
            max_length = max(max_length, len(diff))
            
        return max_length
            
        
        
        
        
        

'알고리즘 문제풀기' 카테고리의 다른 글

647. Palindromic Substrings  (0) 2020.02.04
221. Maximal Square  (0) 2020.02.04
120. Triangle  (0) 2020.01.28
39. Combination Sum  (0) 2020.01.28
950. Reveal Cards In Increasing Order  (0) 2020.01.26