Question
Given a string, return the length of the largest "block" in the string. A block is a run of adjacent chars that are the same.
maxBlock("hoopla") → 2 maxBlock("abbCCCddBBBxx") → 3 maxBlock("") → 0 |
Answer
int len = str.length();
int count = 0;
int tempCount = 1;
if(len==0) return 0;
for(int i=0; i<len;i++){
if(i+1<len && (str.charAt(i)==str.charAt(i+1))){
tempCount++;
}else{
tempCount=1;
}
count = Math.max(count, tempCount);
}
return count;
Reference
http://codingbat.com/prob/p179479http://www.javaproblems.com/2013/11/java-string-3-maxblock-codingbat.html
No comments :
Post a Comment