ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ „๋ฌธ๊ฐ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ "์–ดํ”ผ์น˜"๋Š” ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์— ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ๋น„์†์‹ค ์••์ถ• ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ฌธ์ž์—ด์—์„œ ๊ฐ™์€ ๊ฐ’์ด ์—ฐ์†ํ•ด์„œ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๊ทธ ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜์™€ ๋ฐ˜๋ณต๋˜๋Š” ๊ฐ’์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ๋” ์งง์€ ๋ฌธ์ž์—ด๋กœ ์ค„์—ฌ์„œ ํ‘œํ˜„ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฐ„๋‹จํ•œ ์˜ˆ๋กœ "aabbaccc"์˜ ๊ฒฝ์šฐ "2a2ba3c"(๋ฌธ์ž๊ฐ€ ๋ฐ˜๋ณต๋˜์ง€ ์•Š์•„ ํ•œ๋ฒˆ๋งŒ ๋‚˜ํƒ€๋‚œ ๊ฒฝ์šฐ 1์€ ์ƒ๋žตํ•จ)์™€ ๊ฐ™์ด ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์€ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž๊ฐ€ ์ ์€ ๊ฒฝ์šฐ ์••์ถ•๋ฅ ์ด ๋‚ฎ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, "abcabcdede"์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด์€ ์ „ํ˜€ ์••์ถ•๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. "์–ดํ”ผ์น˜"๋Š” ์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด์„ 1๊ฐœ ์ด์ƒ์˜ ๋‹จ์œ„๋กœ ์ž˜๋ผ์„œ ์••์ถ•ํ•˜์—ฌ ๋” ์งง์€ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, "ababcdcdababcdcd"์˜ ๊ฒฝ์šฐ ๋ฌธ์ž๋ฅผ 1๊ฐœ ๋‹จ์œ„๋กœ ์ž๋ฅด๋ฉด ์ „ํ˜€ ์••์ถ•๋˜์ง€ ์•Š์ง€๋งŒ, 2๊ฐœ ๋‹จ์œ„๋กœ ์ž˜๋ผ์„œ ์••์ถ•ํ•œ๋‹ค๋ฉด "2ab2cd2ab2cd"๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ 8๊ฐœ ๋‹จ์œ„๋กœ ์ž˜๋ผ์„œ ์••์ถ•ํ•œ๋‹ค๋ฉด "2ababcdcd"๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋•Œ๊ฐ€ ๊ฐ€์žฅ ์งง๊ฒŒ ์••์ถ•ํ•˜์—ฌ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ์˜ˆ๋กœ, "abcabcdede"์™€ ๊ฐ™์€ ๊ฒฝ์šฐ, ๋ฌธ์ž๋ฅผ 2๊ฐœ ๋‹จ์œ„๋กœ ์ž˜๋ผ์„œ ์••์ถ•ํ•˜๋ฉด "abcabc2de"๊ฐ€ ๋˜์ง€๋งŒ, 3๊ฐœ ๋‹จ์œ„๋กœ ์ž๋ฅธ๋‹ค๋ฉด "2abcdede"๊ฐ€ ๋˜์–ด 3๊ฐœ ๋‹จ์œ„๊ฐ€ ๊ฐ€์žฅ ์งง์€ ์••์ถ• ๋ฐฉ๋ฒ•์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋•Œ 3๊ฐœ ๋‹จ์œ„๋กœ ์ž๋ฅด๊ณ  ๋งˆ์ง€๋ง‰์— ๋‚จ๋Š” ๋ฌธ์ž์—ด์€ ๊ทธ๋Œ€๋กœ ๋ถ™์—ฌ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์••์ถ•ํ•  ๋ฌธ์ž์—ด s๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์œ„์— ์„ค๋ช…ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ 1๊ฐœ ์ด์ƒ ๋‹จ์œ„๋กœ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ ์••์ถ•ํ•˜์—ฌ ํ‘œํ˜„ํ•œ ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ์งง์€ ๊ฒƒ์˜ ๊ธธ์ด๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • s์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • s๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

sresult

"aabbaccc" 7
"ababcdcdababcdcd" 9
"abcabcdede" 8
"abcabcabcabcdededededede" 14
"xababcdcdababcdcd" 17

์ž…์ถœ๋ ฅ ์˜ˆ์— ๋Œ€ํ•œ ์„ค๋ช…

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

๋ฌธ์ž์—ด์„ 1๊ฐœ ๋‹จ์œ„๋กœ ์ž˜๋ผ ์••์ถ•ํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ์งง์Šต๋‹ˆ๋‹ค.

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

๋ฌธ์ž์—ด์„ 8๊ฐœ ๋‹จ์œ„๋กœ ์ž˜๋ผ ์••์ถ•ํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ์งง์Šต๋‹ˆ๋‹ค.

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

๋ฌธ์ž์—ด์„ 3๊ฐœ ๋‹จ์œ„๋กœ ์ž˜๋ผ ์••์ถ•ํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ์งง์Šต๋‹ˆ๋‹ค.

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

๋ฌธ์ž์—ด์„ 2๊ฐœ ๋‹จ์œ„๋กœ ์ž๋ฅด๋ฉด "abcabcabcabc6de" ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด์„ 3๊ฐœ ๋‹จ์œ„๋กœ ์ž๋ฅด๋ฉด "4abcdededededede" ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด์„ 4๊ฐœ ๋‹จ์œ„๋กœ ์ž๋ฅด๋ฉด "abcabcabcabc3dede" ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด์„ 6๊ฐœ ๋‹จ์œ„๋กœ ์ž๋ฅผ ๊ฒฝ์šฐ "2abcabc2dedede"๊ฐ€ ๋˜๋ฉฐ, ์ด๋•Œ์˜ ๊ธธ์ด๊ฐ€ 14๋กœ ๊ฐ€์žฅ ์งง์Šต๋‹ˆ๋‹ค.

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

๋ฌธ์ž์—ด์€ ์ œ์ผ ์•ž๋ถ€ํ„ฐ ์ •ํ•ด์ง„ ๊ธธ์ด๋งŒํผ ์ž˜๋ผ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ x / ababcdcd / ababcdcd ๋กœ ์ž๋ฅด๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅ ํ•ฉ๋‹ˆ๋‹ค.
์ด ๊ฒฝ์šฐ ์–ด๋–ป๊ฒŒ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ๋„ ์••์ถ•๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๊ฐ€์žฅ ์งง์€ ๊ธธ์ด๋Š” 17์ด ๋ฉ๋‹ˆ๋‹ค.

 

๋ฌธ์ œํ’€์ด

  1. ์ˆœํšŒ ๋ฒ”์œ„ ์ง€์ •: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s๋ฅผ ์ž๋ฅผ ์ˆ˜ ์žˆ๋Š” ๋‹จ์œ„๋Š” ์ตœ์†Œ 1 ~ ์ตœ๋Œ€ ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด
  2. ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ: ์ธ๋ฑ์Šค ๊ฐ’๋งŒํผ ๋ฌธ์ž์—ด ์š”์†Œ๋ฅผ ๊ตฌ๋ถ„ ์ง“์€ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ
  3. ๋ฌธ์ž์—ด ๋น„๊ต: ํ˜„์žฌ์™€ ์ด์ „ ์š”์†Œ๊ฐ€ ๊ฐ™์€ ๋ฌธ์ž์ด๋ฉด ์ž„์˜ ๋ณ€์ˆ˜ cnt ๊ฐ’ ๊ฐ€์‚ฐ, ๋‹ค๋ฅธ ๋ฌธ์ž์ด๋ฉด ์ž„์˜ ๋ณ€์ˆ˜ cnt์™€ ํ˜„์žฌ ์š”์†Œ ๋ฌธ์ž๋ฅผ ์ €์žฅ

์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ์š”์†Œ๋ฅผ ์ง“๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ณ€์ˆ˜์ธ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๋™์ ์œผ๋กœ ๋ฐ›์•„๋“ค์ด๊ฒŒ๋” ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ, ๊ธฐ์กด ์ •๊ทœ ํ‘œํ˜„ ๋ฆฌํ„ฐ๋Ÿด ํ˜•์‹์œผ๋ก  ๋ณ€์ˆ˜๋ฅผ ๋‚ด๋ถ€์— ํ• ๋‹นํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.

 

์ด์— ๊ฒ€์ƒ‰ํ•ด๋ณธ ๊ฒฐ๊ณผ ๋‹ค์Œ๊ณผ ๊ฐ์ฒด ์ƒ์„ฑ ํ˜•์‹์œผ๋กœ ์ •๊ทœ ํ‘œํ˜„์‹์„ ์ƒ์„ฑํ•˜๋ฉด ๋ณ€์ˆ˜๋ฅผ ๋™์ ์œผ๋กœ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

var pattern = new RegExp('์ฒดํฌํ•˜๊ณ ์‹ถ์€ํŒจํ„ด๋ฌธ์ž์—ด');

https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression

 

How do you use a variable in a regular expression?

I would like to create a String.replaceAll() method in JavaScript and I'm thinking that using a regex would be most terse way to do it. However, I can't figure out how to pass a variable in to a r...

stackoverflow.com

 

๋ฌธ์ž์—ด์„ ๋น„๊ตํ•  ๋•Œ ์–ด๋–ค ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ๋ ์ง€ ๊ณ ๋ฏผ์ด ๋˜์—ˆ๋Š”๋ฐ ์ด์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ž์—ด ์••์ถ•์ด ์ƒ๊ฐ์ด ๋‚˜์„œ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ•œ๋ฒˆ ๋” ํ’€์–ด๋ณธ ๋’ค, ํ•ด๋‹น ๋ฌธ์ œ์— ๋น„์Šทํ•œ ์—ฐ์‚ฐ์‹์œผ๋กœ ์ ์šฉํ•˜์˜€๋‹ค.

 

https://choi95.tistory.com/45?category=881084

 

๋ฌธ์ž์—ด ์••์ถ•

๋ฌธ์ œ ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์•„ ๊ฐ™์€ ๋ฌธ์ž๊ฐ€ ์—ฐ์†์œผ๋กœ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž ๋ฐ”๋กœ ์˜ค๋ฅธ์ชฝ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๊ณ ์ž ํ•œ๋‹ค. (๋‹จ ๋ฐ˜๋ณต ํšŸ์ˆ˜

choi95.tistory.com

 

์ฝ”๋“œ

function solution(s) {
  var answer = Number.MAX_SAFE_INTEGER;

  for(let i = 0; i < s.length; i++) {
    let re = new RegExp(`\\D{${i + 1}}`, 'g'); //ํ˜„์žฌ ์ธ๋ฑ์Šค ๋‹จ์œ„๋งŒํผ ๋ฌธ์ž ์ž๋ฅด๊ธฐ
    let pattern = s.match(re); //์ž๋ฅธ ๋‹จ์œ„๋งŒํผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ
    let cnt = 1;
    let tmp = '';
    if(s !== pattern.join('')) pattern.push(s.substring(s.length - (s.length - pattern.join('').length))); 
      //์ž๋ฅด๊ณ  ๋‚จ์€ ๋ฌธ์ž์—ด ๋„ฃ์–ด ์ฃผ๊ธฐ
    for(let i = 0; i < pattern.length; i++) {
      if(pattern[i] === pattern[i + 1]) cnt++;
      else {
        if(cnt > 1) {
          tmp += String(cnt);
          tmp += pattern[i];  
        }
        else tmp += pattern[i];
        cnt = 1;
      }
    }
    let cp = tmp.length;
    answer = Math.min(answer, cp);
  }

  return answer;
}

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ