Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png img_3.png

์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ ์ฒ˜๋Ÿผ ๊ด„ํ˜ธ ์กฐํ•ฉ์„ ํšŒ์ „ ์‹œ์ผฐ์„ ๋•Œ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ์กฐํ•ฉ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋งŒํผ ๋งจ ์•ž ๊ด„ํ˜ธ๋ฅผ ๋งจ ๋’ค๋กœ ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณต ํ•˜๋ฉด์„œ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

ํ’€์ด

์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„  ์—ฌ๋Š” ๊ด„ํ˜ธ('(', '{', '[') ์™€ ๋‹ซ๋Š” ๊ด„ํ˜ธ๊ฐ€ ์ง์„ ์ด๋ค„์•ผ ํ•œ๋‹ค. ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ฌ๋•Œ๋งˆ๋‹ค ์Šคํƒ์— ๋„ฃ์–ด๋’€๋‹ค๊ฐ€ ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด ์Šคํƒ์— ๋„ฃ์–ด๋‘์—ˆ๋˜ ์—ฌ๋Š” ๊ด„ํ˜ธ๋ฅผ ๊บผ๋‚ธ๋‹ค. ์Šคํƒ์ด ๋ชจ๋‘ ๋น„์—ˆ๋‹ค๋ฉด ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅ๋œ ๊ด„ํ˜ธ๊ฐ€ ๋ชจ๋‘ ์˜ฌ๋ฐ”๋ฅด๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ answer ๋ฅผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค. ์ด ์ž‘์—…์„ ๋ฌธ์ž์—ด์„ ํšŒ์ „ํ•˜๋ฉด์„œ ๊ณ„์† ๋ฐ˜๋ณตํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
    public int solution(String s) {
    int answer = 0;
    for (int i = 0; i < s.length(); i++) {
      String s1 = s.substring(i) + s.substring(0, i);
      if (check(s1)) {
        answer++;
      }
    }
    return answer;
  }

  boolean check(String s) {
    ArrayDeque<Character> stack = new ArrayDeque<>();
    char[] charArray = s.toCharArray();
    for (char ch : charArray) {
      if (ch == '(' || ch == '[' || ch == '{') {
        stack.push(ch);
      } else if (ch == ')') {
        if (stack.isEmpty() || !stack.pop().equals('(')) {
          return false;
        }
      } else if (ch == ']') {
        if (stack.isEmpty() || !stack.pop().equals('[')) {
          return false;
        }
      } else if (ch == '}') {
        if (stack.isEmpty() || !stack.pop().equals('{')) {
          return false;
        }
      }
    }
    return stack.isEmpty();
  }
}
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ