728x90
반응형
목차
문제
https://school.programmers.co.kr/learn/courses/30/lessons/60057
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
문제 구현 방향
문자열은 제일 앞부터 정해진 길이만큼 잘라야 한다는 조건을 제대로 보지 않아 헤맸던 문제이다.
따라서 크기별로 차례차례 현재 다음 값을 천천히 비교해 주면된다.
인덱스 1부터 시작 이런거는 생각할 필요가 없었다..
문제 구현
function solution(s) {
var answer = "";
const candidate = [];
const Len = s.length;
for(let unit=1; unit<= Len; unit++){
for(let i=0, cnt =1; i< Len; i +=unit ){
const cur = s.slice(i, i+unit);
const next = s.slice(i+unit, i+2*unit);
if(cur === next){
cnt++;
}else{
let str = cnt ===1 ? '' : cnt;
answer += `${str}${cur}`;
cnt = 1;
}
}
candidate.push(answer.length);
answer ="";
}
return Math.min(...candidate);
}
반응형
'PS > 프로그래머스' 카테고리의 다른 글
| [프로그래머스][브루트포스] 방문 길이 (0) | 2025.11.27 |
|---|---|
| [프로그래머스][dfs] 후보키 (0) | 2025.11.26 |
| [프로그래머스][수학] 2개 이하로 다른 버튼 (0) | 2025.11.19 |
| [프로그래머스][unionFind] 전력망을 둘로 나누기 (0) | 2025.11.17 |
| [프로그래머스][수학] n^2 배열 자르기 (0) | 2025.11.08 |