728x90
SMALL

C#

 

 

 

using System;
using System.Collections.Generic;

public class Solution {
    public int solution(string s) {
        int answer = 0;
        int sameIndex = 0;
        int diffIndex = 0;
        char word = ' ';
        bool start = true; 
        
        for(int i = 0; i < s.Length; i++)
        {
            if(start == true)
            {
                word = s[i];
                sameIndex++;
                start = false;
                // 첫 번째 글자
            }
            else
            {
                if(s[i] == word)
                {
                    sameIndex++;
                }
                else
                {
                    diffIndex++;
                }
                // 첫 번째 글자 word랑 같으면 same을 플러스하고 아니면 다른 인덱스 플러스
            }
            
            if(sameIndex == diffIndex)
            {
                answer++;
                sameIndex = 0;
                diffIndex = 0;
                start = true;
                /// 첫 글자와 같은 문자의 수 = 다른 문자의 수이면 
                /// 자른 후에 초기화 하기
            }
            
            if(i == s.Length - 1)
            {
                if(start == false)
                {
                    answer++;
                }
            }
            /// 마지막 글자에 +1을 해준다.
        }
        return answer;
    }
}

 

 

//////////////////////

 

 

 

using System;

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

        if (s == string.Empty)
                return answer;

            char frist = s[0];

            int sameCount = 0;
            int diffCount = 0;

            for (int i = 0; i < s.Length; ++i)
            {
                if(sameCount == 0)
                    frist = s[i];

                if (frist == s[i])
                    sameCount++;
                else
                    diffCount++;

                if (sameCount == diffCount)
                {
                    answer++;

                    sameCount = 0;
                    diffCount = 0;
                }
                else
                {
                    if(i + 1 >= s.Length)
                        answer++;
                }
            }


        return answer;
    }
}

 

 

 

자바

 

 

 

public class Solution {
    public int solution(String s) {
        int answer = 0;
        char init = s.charAt(0);
        int count = 0;
        for (char c : s.toCharArray()) 
        {
            if (count == 0) 
            {
                init = c;
            }
            if (init == c) 
            {
                count++;
            } 
            else 
            {
                count--;
            }
            
            if (count == 0) 
            {
                answer++;
            }
        }

        if(count > 0) 
        {
            answer++;
        }
        return answer;
    }
}
728x90

설정

트랙백

댓글