알고리즘
leetcode, 1446. Consecutive Characters
LimCoz
2021. 12. 13. 19:24
The power of the string is the maximum length of a non-empty substring that contains only one unique character.
Given a string s, return the power of s.
Example 1:
Input: s = "leetcode"
Output: 2
Explanation: The substring "ee" is of length 2 with the character 'e' only.
문제 : 연속된 문자의 최대 갯수구하는 문제
class Solution {
public int maxPower(String s) {
int l = 0;
int r = s.length()-1;
int power = 0;
while(l <= r){
int tl = l;//밑에 while 문에 사용할 임시 left 인덱스
int ch = s.charAt(l);
int cnt = 0;
//현재 l 인덱스 값이 다음 인덱스에도 있는지 확인해본다.
while(tl <= r){
if(ch != s.charAt(tl)) break;
cnt++; //같은 문자있을경우
tl++; //임시 인덱스 ++
}
power = Math.max(power, cnt);
l++;
}
return power;
}
}