Social Icons

Wednesday, May 12, 2010

maxBlock : solutions for Javabat :18



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


source

solution:

public int maxBlock(String str) {
  int block =1 , largestBlock=0 ;
  
  if(str.length()==0) return 0;
  
  for(int i=0 ; i<str.length(); i++){
     while(i+1<str.length() && str.charAt(i)==str.charAt(i+1) ){
     
         block++;i++;
             
     }
     
     if(block>largestBlock) largestBlock=block;
     block=1;
  }
  return largestBlock;
}
 
 
Blogger Templates http://slots.to/