Coding Test/JAVA 코딩테스트 풀이정리(프로그래머스)

프로그래머스 스쿨 Lv.1 - 문자열 나누기(String.split)
깝몬 2024. 1. 7. 16:51

문제

 

https://school.programmers.co.kr/learn/courses/30/lessons/140108

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

정답

class Solution {
    public int solution(String s) {
        String[] split = s.split("");
        int xCnt=0;
        int oCnt=0;
        int answer = 1;
        String save = "";
        for(int i=0;i<split.length-1;i++){
            //시작할때 첫문자 저장하고 카운트 1 올리기
            if(xCnt==0){
                save=split[i];
                xCnt=1;
                continue;
            }
            
            //시작이 아니라면 그문자와 save가 같은지 비교하기
            if(save.equals(split[i])){
                xCnt++; 
            }else{
                oCnt++;
            }
            
            //만약 두수가 같다면 자른셈치고 xCnt초기화
            if(xCnt==oCnt){
                answer++;
                xCnt=0;
                oCnt=0;
            }
        }
        
        return answer;
    }
}

 

문자열을 순차적으로 검사하면서 초기화와 개수 세는 로직을 만드는 문제다.

 

특별한점은 없어보인다.