[LeetCode] Consecutive Characters

아르당·약 15시간 전

LeetCode

목록 보기
302/303
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

문자열의 power는 하나의 고유 문자만 포함하고 비어 있지 않은 부분 문자열의 최대 길이이다.

문자열 s가 주어졌을 때, s의 power를 반환해라.

Example

#1
Input: s = "leetcode"
Output: 2
Explanation: 부분 문자열 "ee"는 문자 'e'만 가지는 길이 2이다.

#2
Input: s = "abbcccddddeeeeedcba"
Output: 5
Explanation: 부분 문자열 "eeeee"는 문자 'e'만 가지는 길이 5이다.

Constraints

  • 1 <= s.length <= 500
  • s는 오직 영어 소문자로 구성된다.

Solved

class Solution {
    public int maxPower(String s) {
        int result = 1;
        int current = 1;

        for(int i = 0; i < s.length() - 1; i++){
            if(s.charAt(i) == s.charAt(i + 1)){
                current++;
                result = Math.max(result, current);
            }else{
                current = 1;
            }
        }

        return result;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글