๐Ÿ”ฅ[99ํด๋Ÿฝ ์ฝ”ํ…Œ ์Šคํ„ฐ๋””] 28์ผ์ฐจ TIL - ๊ด„ํ˜ธ ํšŒ์ „ํ•˜๊ธฐ

HOONSSACยท2024๋…„ 8์›” 18์ผ
1

99Club Coding Test Study

๋ชฉ๋ก ๋ณด๊ธฐ
28/41
post-thumbnail
post-custom-banner

โณ๋ฌธ์ œ

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

  • (), [], {} ๋Š” ๋ชจ๋‘ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ A๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, (A), [A], {A} ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, [] ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ, ([]) ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ A, B๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, AB ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, {} ์™€ ([]) ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ฏ€๋กœ, {}([]) ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

๋Œ€๊ด„ํ˜ธ, ์ค‘๊ด„ํ˜ธ, ๊ทธ๋ฆฌ๊ณ  ์†Œ๊ด„ํ˜ธ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด s๋ฅผ ์™ผ์ชฝ์œผ๋กœ x (0 โ‰ค x < (s์˜ ๊ธธ์ด)) ์นธ๋งŒํผ ํšŒ์ „์‹œ์ผฐ์„ ๋•Œ s๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ๋˜๊ฒŒ ํ•˜๋Š” x์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

๐Ÿšจ์ œํ•œ ์‚ฌํ•ญ

  • s์˜ ๊ธธ์ด๋Š” 1์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

๐Ÿ“ƒ์ž…์ถœ๋ ฅ ์˜ˆ

sresult
"{}"3
"}]()[{"2
"[)(]"0
"}}}"0

๐Ÿ“ƒ์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ#1

  • ๋‹ค์Œ ๋˜๋Š” "[](){}" ๋ฅผ ํšŒ์ „์‹œํ‚จ ๋ชจ์Šต์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
xs๋ฅผ ์™ผ์ชฝ์œผ๋กœ x์นธ๋งŒํผ ํšŒ์ „์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด?
0"{}"O
1"](){}["X
2"(){}[]"O
3"){}[]("X
4"{}"O
5"}{"X
  • ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ๋˜๋Š” x๊ฐ€ 3๊ฐœ์ด๋ฏ€๋กœ, 3์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ#2

  • ๋‹ค์Œ ๋˜๋Š” "}]()[{" ๋ฅผ ํšŒ์ „์‹œํ‚จ ๋ชจ์Šต์„ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
xs๋ฅผ ์™ผ์ชฝ์œผ๋กœ x์นธ๋งŒํผ ํšŒ์ „์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด?
0"}]()[{"X
1"]()[{}"X
2"()[{}]"O
3")[{}]("X
4"{}"O
5"{}]()["X
  • ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ๋˜๋Š” x๊ฐ€ 2๊ฐœ์ด๋ฏ€๋กœ, 2๋ฅผ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ#3

  • s๋ฅผ ์–ด๋–ป๊ฒŒ ํšŒ์ „ํ•˜๋”๋ผ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, 0์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ#4

  • s๋ฅผ ์–ด๋–ป๊ฒŒ ํšŒ์ „ํ•˜๋”๋ผ๋„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, 0์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

โœ๏ธํ’€์ด

1. ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ํšŒ์ „์‹œํ‚ฌ๊นŒ

์šฐ์„ , ๋ฌธ์ž์—ด s๋ฅผ ํšŒ์ „์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด ๋ณด์•˜๋‹ค.

s๊ฐ€ ๋งŒ์•ฝ "()[]{}"๋ผ๋ฉด, ๊ณ ๋ คํ•ด์•ผ ํ•  ๋ฌธ์ž์—ด๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • "()[]{}"
  • ")[]{}("
  • "[]{}()"
  • "]{}()["
  • "{}()[]"
  • "}()[]{"

์ด๋ ‡๊ฒŒ ์ด 6๊ฐ€์ง€์˜ String์„ ๋ฐ˜๋ณต์ ์ธ ๊ทœ์น™์„ ํ†ตํ•ด ๊ตฌํ•ด์•ผ ํ•˜๋Š”๋ฐ,
์ด ๋•Œ substring() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
substring() ๋ฉ”์„œ๋“œ๋Š” String ๋ฌธ์ž์—ด์„ ์ผ์ • ๊ตฌ๊ฐ„ ์Šฌ๋ผ์ด์‹ฑ ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š”๋ฐ,
๊ธฐ์กด์˜ ๋ฌธ์ž์—ด s์—์„œ ์ธ๋ฑ์Šค๋ฅผ ์ž˜ ํ™œ์šฉํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ฌธ์ž์—ด์„ ํšŒ์ „์‹œํ‚ค๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

String str = s.substring(i, s.length()) + s.substring(0, i);

์ด๋Ÿฐ ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด, ์™ผ์ชฝ์œผ๋กœ i์นธ์”ฉ ํšŒ์ „ํ•˜๋Š” ์ƒˆ๋กœ์šด str๋ฅผ ๋งŒ๋“ค ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

2. ๊ด„ํ˜ธ์˜ ์ง์„ ํ™•์ธํ•ด ๋ณด์ž

์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ธ์ง€ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜๋Š” stack์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.
๋ฌธ์ž์—ด str์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ,
์—ฌ๋Š” ๊ด„ํ˜ธ (, {, [ ์˜ ๊ฒฝ์šฐ์—๋Š” stack์— ์ €์žฅํ•ด์ค€๋‹ค.
๋‹ซ๋Š” ๊ด„ํ˜ธ ), }, ] ์˜ ๊ฒฝ์šฐ์—๋Š” peek()์œผ๋กœ ์—ด๋ฆฐ ๊ด„ํ˜ธ๊ฐ€ stack์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์ง€ ํ™•์ธ ํ›„, ์ง์ด ๋งž๋Š” ์—ด๋ฆฐ ๊ด„ํ˜ธ๊ฐ€ ์žˆ๋‹ค๋ฉด pop() ํ•ด์ค€๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ, stack์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ peek()ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ํ˜„์žฌ ๊ด„ํ˜ธ๋ž‘ ์ง์ด ๋งž์ง€ ์•Š๋‹ค๋ฉด, ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๊ฒŒ ๋œ๋‹ค.
์ด ๊ฒฝ์šฐ๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ฌด์กฐ๊ฑด ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๋Š” correct๋ณ€์ˆ˜์—๋‹ค๊ฐ€ false๋ฅผ ์ €์žฅํ•ด ์ค€๋‹ค.

๐Ÿ‘พ์ตœ์ข… ์ฝ”๋“œ

public static int solution(String s) {
        int answer = 0;

        for (int i = 0; i < s.length(); i++) {
            // ์Šคํƒ ์ƒ์„ฑ
            Stack<Character> stack = new Stack<>();
            
			// ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜
            boolean correct = true;

            // ํšŒ์ „ ์ค‘ ํ˜„์žฌ ๋ฌธ์ž์—ด
            String str = s.substring(i, s.length()) + s.substring(0, i);

            for (int j = 0; j < s.length(); j++) {
                char temp = str.charAt(j);
                switch (temp) {
                    case '(':
                        stack.push(temp);
                        break;
                    case '{':
                        stack.push(temp);
                        break;
                    case '[':
                        stack.push(temp);
                        break;
                    case ')':
                        if (stack.isEmpty() || stack.peek() != '(') {
                            correct = false;
                            break;
                        }
                        else {
                            stack.pop();
                            break;
                        }
                    case '}':
                        if (stack.isEmpty() || stack.peek() != '{') {
                            correct = false;
                            break;
                        }
                        else {
                            stack.pop();
                            break;
                        }
                    case ']':
                        if (stack.isEmpty() || stack.peek() != '[') {
                            correct = false;
                            break;
                        }
                        else {
                            stack.pop();
                            break;
                        }
                }
            }

			// ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ
            if (stack.isEmpty() && correct == true) {
                answer++;
            }
            
            // stack ์ดˆ๊ธฐํ™”
            while (!stack.isEmpty()) {
                stack.pop();
            }

        }
        return answer;
    }

์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ, correct ๋ณ€์ˆ˜์—๋Š” ์ฒ˜์Œ์— ์ €์žฅ๋œ true๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์กฐ๊ฑด๋ฌธ์„ ๋งŒ๋“ค์–ด answer์— ์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ์ฃผ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ๋‹ค์Œ str์„ ํ™•์ธํ•˜๋Ÿฌ ๊ฐ€๊ธฐ ์œ„ํ•ด stack์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๊ฒƒ๋„ ์žŠ์ง€ ์•Š๊ณ  ํ•ด์ฃผ์—ˆ๋‹ค.

๐Ÿ‘พ๋” ๋‚˜์€ ์ฝ”๋“œ

import java.util.Stack;

public class Solution {
    static public int solution(String s) {
        int answer = 0;

        for (int i = 0; i < s.length(); i++) {
            Stack<Character> stack = new Stack<>();
            String str = s.substring(i, s.length()) + s.substring(0, i);
            for (int j = 0; j < str.length(); j++) {
                char c = str.charAt(j);
                if (stack.isEmpty()) {
                    stack.push(c);
                } else if (c == ')' && stack.peek() == '(') {
                    stack.pop();
                } else if (c == '}' && stack.peek() == '{') {
                    stack.pop();
                } else if (c == ']' && stack.peek() == '[') {
                    stack.pop();
                } else {
                    stack.push(c);
                }
            }
            if (stack.isEmpty()) {
                answer++;
            }
        }

        return answer;
    }
}

์—ฌ๋Š” ๊ด„ํ˜ธ์˜ ๊ฒฝ์šฐ ๋ฌด์กฐ๊ฑด stack์— push()ํ•ด์ฃผ๋ฉด ๋˜๊ณ ,
๋‹ซ๋Š” ๊ด„ํ˜ธ์˜ ๊ฒฝ์šฐ ๋ฌด์กฐ๊ฑด peek()๊ฐ’์ด๋ž‘ ๊ฐ™๋‹ค๋ฉด ์ฆ‰, ์ง์ด ๋งž๋‹ค๋ฉด pop()ํ•ด์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ณตํ†ต๋˜๋Š” ๋ถ€๋ถ„์„ ์ด๋ ‡๊ฒŒ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ค„์ผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ผ ๊ฒฝ์šฐ stack์€ ๋น„์–ด์žˆ๊ฒŒ ๋˜๊ณ ,
๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” stack์— ์š”์†Œ๊ฐ€ ๋‚จ์•„์žˆ๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์—,correct ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  stack์ด ๋น„์–ด์žˆ๋Š” ์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋งŒ์œผ๋กœ๋„ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€ ์•„๋‹Œ์ง€ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ”—๋ฌธ์ œ ๋งํฌ
๐Ÿ’ปRepository

profile
ํ›ˆ์‹น์˜ ๊ฐœ๋ฐœ์—ฌํ–‰
post-custom-banner

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2024๋…„ 8์›” 19์ผ

๋ฌด์Šจ ์†Œ๋ฆฐ๊ฐ€ ์‹ถ์–ด ๋ฌธ์ œ๋งŒ 30๋ฒˆ ์ฝ์—ˆ๋‹ค

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ