Java > String-3 > maxBlock (CodingBat Solution)

Problem:

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


Solution:

public int maxBlock(String str) {
  int len = str.length();
  int count = 0;
  int tmpcount = 1;
  
  if (len == 0)
    return 0;
  
  for (int i = 0; i < len; i++) {
    if (i < len-1 && str.charAt(i) == str.charAt(i+1))
      tmpcount++;
    else
      tmpcount = 1;
    
    if (tmpcount > count)
      count = tmpcount;
  }
  return count;
}


8 comments :

  1. public int maxBlock(String str) {
    int l = str.length();
    if (l==0) return 0;
    int block = 1;
    int tempblock = 1;
    char same = str.charAt(0);

    for (int i = 1; i<l; i++) {
    if (str.charAt(i) == same) {
    tempblock ++;
    } else {
    same = str.charAt(i);
    tempblock = 1;
    }
    block = Math.max(block, tempblock);
    }

    return block;

    }

    ReplyDelete
  2. public int maxBlock(String str) {
    int maxBlock = 0, sayac = 0;
    char harfDeg = 'x';

    for (int i = 0; i < str.length(); i++) {
    harfDeg=str.charAt(i);
    for (int j = i; j < str.length(); j++) {
    if (harfDeg==str.charAt(j)){
    sayac++;
    }else {
    break;
    }
    }
    maxBlock=Math.max(sayac,maxBlock);
    sayac=0;
    }

    return maxBlock;
    }

    ReplyDelete
  3. String sub = "";
    int ln = 0;
    for(int i = 0;i<str.length();i++)
    {
    sub = "";
    for(int j = i;j<str.length();j++){
    if(str.charAt(j)!=str.charAt(i)){
    i = j-1;
    break;
    }
    sub+=str.charAt(j);
    }
    if(ln<sub.length())ln = sub.length();
    }
    return ln;

    ReplyDelete
  4. public int maxBlock(String str) {
    int maxCount=0;

    for(int j=0; j<str.length()-1;j++){
    int count=1;
    for(int i=j+1; i<str.length();i++){
    if(str.charAt(j)==str.charAt(i)){
    count++;
    }

    else{
    break;
    }

    }

    if(maxCount<count)
    maxCount=count;

    }
    return maxCount;
    }

    ReplyDelete
  5. public int maxBlock(String str) {
    int maxBlock = 0, result = 0;
    char previousChar = 0;
    for(final char c : str.toCharArray()) {
    maxBlock = (c != previousChar)? 1 : maxBlock + 1;
    result = (result < maxBlock)? maxBlock: result;
    previousChar = c;
    }
    return result;
    }

    ReplyDelete
  6. public int maxBlock(String str) {
    int maxBlock = 0, result = 0;
    char previousChar = 0;
    for(final char c : str.toCharArray()) {
    maxBlock = (c != previousChar)? 1 : maxBlock + 1;
    result = (result < maxBlock)? maxBlock: result;
    previousChar = c;
    }
    return result;
    }

    ReplyDelete
  7. for python makers :D :
    def maxBlock(s):
    c = 0
    c1 = 0
    for i in range(len(s)):
    c = 0
    for j in range(len(s)):

    if s[i] == s[j]:
    c += 1
    if c1 < c:
    c1 = c
    return c1

    ReplyDelete
    Replies
    1. fixed
      def maxBlock(s):
      c = 0
      c1 = 0
      for i in range(len(s)-1):

      c = 0
      for j in range(i,len(s)):

      if s[i] != s[j]:
      break
      if s[i] == s[j]:
      c += 1
      if c1 < c:
      c1 = c
      return c1

      Delete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact