Deep_Dev
article thumbnail


์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ์ž˜๋ชป ์ฝ์–ด์„œ ์ข€ ๋Œ์•„๊ฐ”๋Š”๋ฐ..

๊ทธ๋ƒฅ ๋“ฑ์žฅํ•œ ๋ฌธ์ž๋งˆ๋‹ค ๊ฐ™์€ ํšŸ์ˆ˜๋กœ ๋‚˜์˜จ๊ฒŒ ์žˆ๋‹ค๋ฉด ๋ฐ”๋กœ ์นด์šดํŠธ ํ•ด์ฃผ๋Š”์ค„ ์•Œ์•˜๋Š”๋ฐ,

๋ฌด์กฐ๊ฑด '์ฒซ๋ฌธ์ž์™€ ๋‚˜๋จธ์ง€ ๋ฌธ์ž์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜'๋กœ ํŒ๋‹จํ•œ๋‹ค.

 

aaabbcab -> aaabbc / ab

aabcabcd -> aabc / ab / cd 

 

  1. ๋ฌธ์ž์—ด์— ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์ง€์ • ํ›„ ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž๋ถ€ํ„ฐ for ๋ฌธ
  2. ๊ฐ™์„ ๊ฒฝ์šฐ start++, ์•„๋‹ ๊ฒฝ์šฐ end++, ๋งŒ์•ฝ ๋‘ count๊ฐ€ ๊ฐ™๋‹ค๋ฉด break
  3. ๋‹ค ๋Œ์•„๊ฐ„ count ์ˆ˜์˜ ํ•ฉ ๋งŒํผ ๋ฌธ์ž์—ด์„ ์ž๋ฅด๊ธฐ
  4. 1-2-3 ์˜ ์ˆœ์„ ๋ฌธ์ž์—ด์ด ์—†์–ด์งˆ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณต

 

class Solution {
    public int solution(String s) {
       int answer = 0;

       while(s.length()!=0){
           answer++;
           char ch = s.charAt(0);
           int start=1;
           int end=0;
           for(int i=1; i<s.length(); i++){
               if(ch==s.charAt(i)) start++;
               else end++;

               if(start==end) break;
           }
           s=s.substring(start+end);
       }
       return answer;
    }
}