๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฆฌํŠธ์ฝ”๋“œ

[๋ฆฌํŠธ์ฝ”๋“œ] 76. Minimum Window Substring (์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ)

by Jouureee 2021. 8. 5.

๋ฌธ์ œ :

https://leetcode.com/problems/minimum-window-substring/

 

Minimum Window Substring - 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

 

๋‚œ์ด๋„ : hard

 

ํ’€์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜ : sliding window(์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ)

 

โญ๏ธ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋ž€ ? 

์ผ์ • ๋ฒ”์œ„ ๋งŒํผ์˜ ์œˆ๋„์šฐ(w)๋ฅผ ๊ฐ€์ง€๊ณ  ์Šฌ๋ผ์ด๋”ฉ ~ ํ•˜๋ฉฐ ์ฐพ๋Š” ๊ธฐ๋ฒ•์„ ๋งํ•ฉ๋‹ˆ๋‹ค. 

์ฃผ๋กœ ๊ตฌ๊ฐ„ ๋ฌธ์ œ์— ๋งŽ์ด ์‚ฌ์šฉ๋˜๋ฉฐ ํˆฌํฌ์ธํ„ฐ์™€ ๋ฐฉ๋ฒ•๋ก ์ด ๋น„์Šทํ•˜์—ฌ ๊ฐ™์ด ์–˜๊ธฐ ๋ฉ๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ํˆฌํฌ์ธํ„ฐ๋Š” 2๊ฐœ์˜ ํฌ์ธํ„ฐ(left, right)๋ฅผ ๊ฐ€์ง€๊ณ  ๊ตฌ๊ฐ„์˜ ๊ธธ์ด๊ฐ€ ๋‹ค์ด๋‚˜๋ฏนํ•˜๊ฒŒ ์›€์ง์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ์˜ ๊ฒฝ์šฐ ์„œ๋ธŒ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ํ•ญ์ƒ ์ผ์ •ํ•˜๋‹ค๋Š” ์ ์—์„œ ๋‹ค๋ฅธ ๋งค๋ ฅ์ ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

๋ฐ”๋กœ ๋ฐ”๋กœ

 

๐ŸŸจ๐ŸŸจ๐ŸŸจ ์œˆ๋„์šฐ์˜ ๊ธธ์ด๊ฐ€ w = 3 ๋งŒํผ ๊ณ ์ •๋˜์–ด ๊ฐ„๋‹ค ํ–ˆ์„ ๋•Œ 

 โฌ›๏ธโฌ›๏ธ๐ŸŸจ๐ŸŸจ๐ŸŸจโฌ›๏ธโฌ›๏ธโฌ›๏ธ -> โฌ›๏ธโฌ›๏ธ๐ŸŸฅ๐ŸŸจ๐ŸŸจ๐ŸŸฉโฌ›๏ธโฌ›๏ธ 

์ƒˆ๋กœ์šด ์„œ๋ธŒ ๋ฐฐ์—ด์€ ๐ŸŸจ๐ŸŸจ๐ŸŸฉ ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

์ด๋•Œ ๐ŸŸจ๐ŸŸจ๋Š” ์ค‘๋ณต ์‚ฌ์šฉ๋˜๋ฏ€๋กœ w-1 ๋งŒํผ์€ ์žฌ์‚ฌ์šฉ ํ•˜๋ฉฐ ๐ŸŸฉ์€ ๋”ํ•˜๊ณ  ๐ŸŸฅ๋Š” ๋นผ์ฃผ๋ฉฐ O(1)์‹œ๊ฐ„๋งŒํผ ์ƒˆ๋กœ์šด ๐ŸŸจ๐ŸŸจ๐ŸŸฉ ๊ฐ’์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

๊ทธ๋ž˜์„œ ์„œ๋ธŒ ์ŠคํŠธ๋ง์ด ๊ตฌ๊ฐ„์„ ๋„๋Š”๋ฐ O(n) ๋งŒํผ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. 

 

๋ฌธ์ œ๋ฅผ ์Šค์Šค๋ก  ํ’€์ง€ ๋ชปํ–ˆ๊ณ  ์ข‹์€ ๊ธฐ๋ฒ•์ธ๊ฑฐ ๊ฐ™์•„์„œ ๋‹ค์Œ์— ์ž˜ ์ด์šฉํ•˜๋ ค๊ณ  ๋ฉ”๋ชจํ•ฉ๋‹ˆ๋‹ค ...

 


s_len๋งŒํผ ๋Œ๋ฉด์„œ letters[s[high]]๊ฐ€ 0๋ณด๋‹ค ํฐ ๊ฐ’๋งŒ count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ๊ตฌ๊ฐ„์˜ ๊ธธ์ด๋ฅผ ์ •ํ•œ๋‹ค.

 

high - low ๊ตฌ๊ฐ„ ๊ธธ์ด ๋งŒํผ์˜ ์„œ๋ธŒ ๋ฐฐ์—ด์— t ๋ฌธ์ž๋“ค์ด ๋‹ค ํฌํ•จ๋˜์—ˆ์„ ์‹œ low๊ฐ’์„ ์•ž์œผ๋กœ ์˜ฎ๊ฒจ ์ตœ์†Œํ•œ์˜ ๊ธธ์ด๋กœ ์ค„์ด๊ณ  min_length์— ์ €์žฅํ•œ๋‹ค. low๋ฅผ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์€ letters map์˜ ๊ฐ’์ด 0๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ!

 

t์— ์—†๋Š” ๋ฌธ์ž๋“ค์€ ์ดˆ๊ธฐ ๊ฐ’์ด 0์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์— count ๊ฐ’์ด t ๊ธธ์ด์™€ ๊ฐ™์•„์ง€๊ธฐ ์ „์— ์ด๋ฏธ ํ•˜๋‚˜์”ฉ ์ค„์–ด๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์กฐ๊ถŒ -1, -2 ๋“ฑ ์ƒ๊ด€ ์—†์ด 0๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ผ ๊ฒƒ

 

t ๋ฌธ์ž ์ค‘ ์„œ๋ธŒ ๋ฐฐ์—ด์— ์ค‘๋ณต๋œ ๋ฌธ์ž๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ window์˜ ๋ฒ”์œ„์—์„œ ์ค„์—ฌ์•ผ ํ•˜๋Š”๋ฐ ์ค‘๋ณต๋œ ๊ฐ’์€ t์— ์—†๋Š” ๋ฌธ์ž์ฒ˜๋Ÿผ ๊ฐ’์ด < 0 ์œผ๋กœ ๋งŒ๋“ค์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— window์˜ ์–‘ ๋๊ฐ’์— t ๋ฌธ์ž์—ด์ด ์žˆ๋Š” ์ตœ์†Œํ•œ์˜ window substring์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.  

 

 

 

c++ ์ฝ”๋“œ :

class Solution {
public:
   string minWindow(string s, string t) {
   	int t_len = t.length();
        int s_len = s.length();
        int maxInt = 2147483647;
        
        unordered_map<char, int> letters; // t ๋ฌธ์ž๊ฐ€ s์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ map ex> A : 1, B : -1, C : -2 ...
        for(int i =0; i < t_len; i++){
           letters[t[i]]++; 
        }
        
        int count = 0; //t ๋ฌธ์ž์—ด ์„œ๋ธŒ ๋ฐฐ์—ด์— ์ถฉ์กฑ ์‹œ
        int low = 0, min_length = maxInt, min_start = 0;    
       
       
        for(int high = 0; high < s_len; high++) {
        
            if(letters[s[high]] > 0) {
                count++; //means that this letter is in t  
            }
            
            letters[s[high]]--; 

            if(count == t_len) { //์กฐ๊ฑด ์ถฉ์กฑ
                while(low < high && letters[s[low]] < 0) {
                    cout << letters[s[low]] <<'\n';
                    letters[s[low]]++;
                    low++; 
                }
                
                if(maxInt > high-low) {
                    min_length = high - (min_start = low) + 1; 
                }
                
                letters[s[low++]]++; //low ์—…๋ฐ์ดํŠธ ์ „ ๋ฌธ์ž ๊ฐ’ ์ฆ๊ฐ€
                count--;
            }
        }
        return min_length == maxInt ? "" : s.substr(min_start, min_length); 
    }
};

 

์ „์— ์Šคํ„ฐ๋”” ์–ธ๋‹ˆ๊ฐ€ ์นด์นด์˜ค ๋ณด์„ ์‡ผํ•‘ ๋ฌธ์ œ๋„ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋ฅผ ์‚ฌ์šฉํ•ด ํ’€๋ฉด ์ข‹์€ ๋ฌธ์ œ๋ผ ์•Œ๋ ค์คฌ๋Š”๋ฐ ํ•จ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค. 

 

           

 

 

๋Œ“๊ธ€