티스토리 뷰
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;
}
}
'알고리즘' 카테고리의 다른 글
leetcode - 56. Merge Intervals (0) | 2021.12.24 |
---|---|
leetocde - 143. Reorder List (0) | 2021.12.22 |
leetcode - 231. Power of Two (0) | 2021.12.21 |
leetcode - 938. Range Sum of BST (0) | 2021.12.14 |
leetcode 1306. Jump Game III (0) | 2021.12.10 |