본문 바로가기
IT/알고리즘

22] Leetcode 1446. Consecutive Characters

by 깻잎쌈 2020. 7. 3.
반응형

 

 

Consecutive Characters - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문자열이 주어지고 그 문자열내에서 한 알파벳으로만 이뤄진 부분 문자열의 최대 길이를 반환하는 문제.

class Solution {
public:
    int maxPower(string s) {
        int ans = 1;
        int startPoint = 0;
        char startChar= s[0];
        
        for(int i = 1;i<s.size();i++){
            if(s[i] != startChar){// 다르면       
                 startPoint = i;
                 startChar = s[i];
            }
            else if(s[i] == startChar){ // 같으면
                if(ans < i-startPoint+1)
                    ans = i-startPoint+1;              
            }                        
        }
        
        return ans;
    }
};

초기값은 1

반응형

댓글