알고리즘 문제 풀기(프로그래머스)
https://github.com/hoinlee-moi/Algorithm
JS기본문법 다시 공부
https://github.com/hoinlee-moi/ModernJS
React 강의 듣기
https://github.com/hoinlee-moi/React_prac
오늘알고리즘
문자열 나누기 - https://school.programmers.co.kr/learn/courses/30/lessons/140108
function solution(s) {
let answer = 0;
let standard = s[0]
let count = [1,0];
for(let i=1;i<=s.length-1;i++){
if(s[i]===standard) count[0]++
else count[1]++
if(count[0]===count[1]){
answer++
standard = s[i+1]
count = [0,0]
}
}
if(count[0]!==0||count[1]!==0) answer++
return answer;
}
단순히 반복문과 조건들을 이용하여 풀었습니다.
속도면에선 빠르지만 코드의 확장성은 부족한 듯 합니다
answer변수를 만듭니다standard변수를 만들고 맨 첫 문자를 할당합니다count란 배열에 2가지 요소만 두고 standard에 미리 첫 문자를 할당하였으니 0번째 요소를 기준 문자라 보고 1을 할당합니다.for를 이용해 반복문을 돕니다 (0번째는 할당하였으니 1번부터 돈다)count에서 0번째 요소의 횟수를 1증가시키고 다르다면 1번째 요소를 증가시킵니다count에서의 0번째와 1번째의 값이 같으면 동일한 값이 되니 정답 카운트 answer를 증가시키고 기준 문자열을 다음 문자열로 할당 count를 다시 [0,0]으로 초기화 시킵니다.[0,0]이 아닌 조건문을 걸어 정답 answer에 증감을 합니다아래는 재귀 함수를 이용한 다른 사람의 풀이인데 참고해봐도 좋을 것 같다.
function solution(s, count=0) {
if(!s) return count
let [first, ...rest] = s.split("")
let countSame = 1
let countInSame = 0
let i=0
for(; i<rest.length; i++){
if(rest[i] === first) countSame++
else countInSame++
if(countSame === countInSame) break
}
return solution(rest.slice(i+1).join(""), count+1)
}