๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

IN DEPTH CAKE/Supercoder

[c++/LeetCode-Stack] 394. Decode String

 

 

 

๋‚œ์ด๋„Medium

ํ‚ค์›Œ๋“œ: Stack

๋ฌธ์ œ

 

์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋””์ฝ”๋”ฉํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ธ์ฝ”๋”ฉ ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค: 

 

k[์ธ์ฝ”๋”ฉ๋œ_๋ฌธ์ž์—ด], ์—ฌ๊ธฐ์„œ ๋Œ€๊ด„ํ˜ธ ๋‚ด์˜ ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์ด k๋ฒˆ ์ •ํ™•ํžˆ ๋ฐ˜๋ณต๋ฉ๋‹ˆ๋‹ค. 
k๋Š” ์–‘์˜ ์ •์ˆ˜์ž„์ด ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค.



์ž…๋ ฅ ๋ฌธ์ž์—ด์ด ํ•ญ์ƒ ์œ ํšจํ•˜๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์ธ ๊ณต๋ฐฑ์ด๋‚˜ ๋Œ€๊ด„ํ˜ธ๊ฐ€ ์ž˜ ํ˜•์„ฑ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์›๋ณธ ๋ฐ์ดํ„ฐ์—๋Š” ์ˆซ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉฐ ์ˆซ์ž๋Š” ๋ฐ˜๋ณต ํšŸ์ˆ˜ k๋งŒ์„ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด, 3a ๋˜๋Š” 2[4]์™€ ๊ฐ™์€ ์ž…๋ ฅ์€ ์—†์Šต๋‹ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์ถœ๋ ฅ์˜ ๊ธธ์ด๊ฐ€ ํ•ญ์ƒ 10^5๋ฅผ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋„๋ก ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

์ž…๋ ฅ ์˜ˆ์‹œ

 

Example 1:

Input: s = "3[a]2[bc]"
Output: "aaabcbc"

 

๋ฌธ์ œ ์›๋ฌธ: https://leetcode.com/problems/decode-string/description/

 

๋ฌธ์ œ ํ’€์ด

 

- ๊ด„ํ˜ธ๊ฐ€ ์žˆ๋Š” ๋ฌธ์ œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ stack ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

- ๊ทธ๋ž˜์„œ stack์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.

- ๋ณ„๋„์˜ stack ์ž๋ฃŒํ˜•์„ ๋‘์ง€ ์•Š๊ณ  vector<char>๋ฅผ ๋‘ฌ์„œ ํ’€์—ˆ๋Š”๋ฐ, top() ์กฐํšŒ๋Š” `stack.back()` ์œผ๋กœ, pop() ์—ฐ์‚ฐ์ž๋Š” `stack.erase(stack.end()-1)`๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

- ์•„์ด๋””์–ด๋Š” ]๋ฅผ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€์˜ ์ •๋ณด๋ฅผ ๋ชจ๋‘ stack ์— ์ €์žฅํ•˜๊ณ , ]๋ฅผ ๋งŒ๋‚˜๋ฉด [๋ฅผ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€์˜ ์ •๋ณด๋ฅผ tmp_stack์— ๋„ฃ์–ด๋‘์—ˆ๋‹ค๊ฐ€. ์ด์–ด์„œ ๋‚˜ํƒ€๋‚˜๋Š” ์ˆซ์ž (์ด ๋•Œ, ํ•œ์ž๋ฆฌ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— while ๋ฌธ์„ ๊ฐ€์ง€๊ณ  ์ˆซ์ž N์„ ์ฐพ์Œ) ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ stack์— ๋„ฃ์–ด์คฌ๋‹ค.

- ์ตœ์ข…์ ์œผ๋กœ stack ์•ˆ์— ๋‹ด๊ฒจ์žˆ๋Š” ๊ฐ’์„ string์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

Runtime: 0ms Beats100.00%of users with C++
Memory: 7.85MB Beats51.63%of users with C++

 

class Solution {
public:
    string decodeString(string s) {

        vector<char> stack;
        for(auto c : s){
            if(c == ']'){
                vector<char> tmp_stack;
                while(stack.back() != '['){
                    // pop from stack and push into tmp_stack
                    tmp_stack.emplace_back(stack.back());
                    stack.erase(stack.end()-1);
                }
                // remove dummy '['
                stack.erase(stack.end()-1);
                // pop number from stack
                int digit = 0;
                int N = 0;
                while(stack.size() > 0 && stack.back() >= 48 && stack.back() <= 58){
                    N += (int(stack.back())-48) * (pow(10,digit++));
                    stack.erase(stack.end()-1);
                }
                cout << N << endl;
                // push substring in tmp_stack to stack N times
                for(int _ = 0; _ < N; ++_){
                    for(auto it = tmp_stack.end()-1; it >= tmp_stack.begin(); it--)
                        stack.emplace_back(*it);
                }
            } else {
                stack.emplace_back(c);
            }
        }

        return string(stack.begin(), stack.end());
    }
};

 

 

 

๋ฐ˜์‘ํ˜•