๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฐฑ์ค€ JS

[๋ฐฑ์ค€] JS/ 4949 ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ

by Tamii 2021. 9. 29.
๋ฐ˜์‘ํ˜•

 

โ–.์‹ค์ œ ๋ฐฑ์ค€ ํ’€์ด์—์„œ๋Š” fs.readFileSync('./test') -> fs.readFileSync('./dev/stdin') ์ด๋ ‡๊ฒŒ ๊ณ ์ณ์„œ ์ƒ๊ฐํ•ด์ฃผ์„ธ์š”

 

 

์ฒ˜์Œ ํ’€์ด๋Š”  ์™„์ „ํžˆ ์ž˜๋ชป ์ƒ๊ฐํ–ˆ๋‹ค.

๋‘๊ฐœ์˜ ๊ฐœ์ˆ˜๋งŒ ๋งž์œผ๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ ์ •์„ฑ์Šค๋Ÿฝ๊ฒŒ ์ด์ƒํ•œ ์ฝ”๋“œ๋ฅผ ์จ๋‚ด๋ ค๊ฐ”๋‹ค..^^

 

์ฒ˜์Œ ํ’€์ด ( ๋ฐฉํ–ฅ ์™„์ „ ์ž˜๋ชป )

const fs = require('fs');

let input = (fs.readFileSync('./test') + '').toString().trim().split('.');

input.pop();
input.pop();

console.log(input);

const regL = /\[/g;
const regR = /\[/g;
const regl = /\(/g;
const regr = /\)/g;

const ans = input.map((i) => {
  const regLCnt = i.match(regL) ? i.match(regL).length : 0;
  const regRCnt = i.match(regR) ? i.match(regR).length : 0;
  const reglCnt = i.match(regl) ? i.match(regl).length : 0;
  const regrCnt = i.match(regr) ? i.match(regr).length : 0;
  console.log(i, 'i', regLCnt, regRCnt, reglCnt, regrCnt);
  if (regLCnt === regRCnt && reglCnt === regrCnt) return 'yes';
  else return 'no';
});

console.log(ans);

 

๊ทธ ๋‹ค์Œ์—์•ผ ๋“  ์ƒ๊ฐ์€

 ์Šคํƒ์„ ๋งŒ๋“ค์–ด์„œ ๋„ฃ๋‹ค ๋บ๋‹ค ํ•ด์ค˜์•ผ ํ•˜๋Š”๊ตฌ๋‚˜ ๋ผ๋Š”๊ฑธ ๋Š๋‚Œ

 

 

 

 

๋‚ดํ’€์ด 2 (์˜ค๋‹ต)

const fs = require('fs');

let input = (fs.readFileSync('./test') + '').toString().trim().split('.');

input.pop();
input.pop();


const ans = input.map((sentence) => {
  const bracketStack = [];
  sentence.split('').forEach((word) => {
    if (word === '(' || word === '[') {
      bracketStack.push(word);
    } else if (word === ')') {
      if (bracketStack[bracketStack.length - 1] !== '(') return 'no';
      else bracketStack.pop();
    } else if (word === ']') {
      if (bracketStack[bracketStack.length - 1] !== '[') return 'no';
      else bracketStack.pop();
    }
  });

  if (bracketStack.length !== 0) return 'no';
  return 'yes';
});
ans.forEach((a) => console.log(a));

์–ด๋–ป๊ฒŒ๋“  ๋‹ต์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ๋ฐœ์•…..?

๋ฌธ์ œ ์˜ˆ์‹œ๋Š” ๋งž์•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ 

ํ—ˆ์ˆ ํ•œ ์กฐ๊ฑด๋ฌธ์—์„œ ๊ฑธ๋ ธ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๊ณ  ํ”Œ๋Ÿฌ์Šค ์‹œ๊ฐ„์ดˆ๊ณผ๋„ ์˜ˆ์ƒํ•˜๊ณ ์žˆ์—ˆ๋‹ค.

 

 

 

 

๋‚ด ํ’€์ด3(์ •๋‹ต)

const fs = require('fs');

let input = (fs.readFileSync('./test') + '').toString().trim().split('\n');
const open = ['(', '['];
const close = [')', ']'];
let bracketStack;
const ans = [];

input.pop();
input.forEach((sentence) => {
  let isBool = false;
  bracketStack = [];

  for (let i = 0; i < sentence.length; i++) {
    if (open.includes(sentence[i])) bracketStack.push(sentence[i]);
    else if (close.includes(sentence[i])) {
      if (bracketStack.pop() !== open[close.indexOf(sentence[i])]) {
        ans.push('no');
        isBool = true;
        break;
      }
    }
  }
  if (!isBool) {
    if (bracketStack.length === 0) ans.push('yes');
    else ans.push('no');
  }
});

console.log(ans.join('\n'));

๋‚˜์˜ฌ๋•Œ๋งˆ๋‹ค ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ.

 

1. ๊ฐ ๋ฌธ์ž์—ด์ด open์ธ์ง€ close์ธ์ง€ ํ™•์ธ

2. close๋ผ๋ฉด ๋ฐฉ๊ธˆ๋‚˜์˜จ ๋ฌธ์ž์—ด์ด ')' ์ธ์ง€ ']'์ธ์ง€ ํ™•์ธ (open๊ณผ close์˜ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™์Œ์„ ์ด์šฉ)

3. 2๋ฒˆ์ด ํ‹€๋ฆด ๊ฒฝ์šฐ isBool๊ฐ’์œผ๋กœ ๋„˜๊ฒจ  bracket์ด ๋น„์–ด์žˆ๋Š”์ง€์˜ ์—ฌ๋ถ€๋กœ ๋‹ต ํ™•์ธ

 

4. ๋งˆ์ง€๋ง‰์œผ๋กœ for๋ฌธ์œผ๋กœ console.log ํ•˜์ง€์•Š๊ณ  join ( ์ด๊ฑฐ ์ž๊พธ ๊นŒ๋จน์Œ)

 

 

---

 

 

๐Ÿฅณ ์ •๋ง ..... ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ์ž๊พธ ๋‹ต์ด ํ‹€๋ฆฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜ค๋Š˜๋„ ์ƒˆ๋กœ์šด ํšจ์šธ์ ์ธ ํ‘œํ˜„๋“ค์„ ๋ฐฐ์› ๋”ฐ.

 

๋Œ“๊ธ€