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;
}

14 comments:

  1. Here's mine.

    public int maxBlock(String str) {
    int o = 0; //Out
    int s = 0; //Local summation
    int len = str.length();
    if (len>0){
    char c = str.charAt(0); //Character
    for (int i=0;i<len+1;i++){
    if (i<len){
    char c2 = str.charAt(i);
    if (c2 == c) {
    s++;
    } else {
    o = Math.max(s,o);
    s = 1;
    c = str.charAt(Math.min(i,len-1));
    }
    } else {
    o = Math.max(s,o);
    }
    }
    }
    return o;
    }

    ReplyDelete
  2. Really Works! Simply Superb!


    I have used map, which is little complex when comes to memory, but logic is easy to understand

    public int maxBlock(String str) {
    int maxLength = 0;
    Map map = new HashMap();
    for(int i = 0; i< str.length(); i++){
    String key = str.substring(i,i+1);
    if(i!=0 && str.charAt(i) == str.charAt(i-1) && map.containsKey(key)){
    map.put(key, map.get(key)+1);
    }
    else{
    map.put(key,1);
    }
    }

    for(Map.Entry entry : map.entrySet()){
    if(maxLength <entry.getValue()){
    maxLength = entry.getValue();
    }
    }

    return maxLength;

    }


    ReplyDelete
  3. public int maxBlock(String str) {

    if(str.length() == 0){
    return 0;
    }

    int maxRecur = 1;
    int finalMaxRecur = 1;
    for(int i = 1; i< str.length(); i++){
    if(str.charAt(i) == str.charAt(i-1)){
    maxRecur++;
    }
    else{
    if(finalMaxRecur < maxRecur){
    finalMaxRecur = maxRecur;
    }
    maxRecur = 1; // reset the value to 1 as this is new char
    }
    }
    if(finalMaxRecur < maxRecur){
    finalMaxRecur = maxRecur;
    }
    return finalMaxRecur;

    }

    ReplyDelete
  4. public int maxBlock(String str) {
    int n=0, res=0;
    char previousc=0;
    for(char c : str.toCharArray()) {
    n = c == previousc ? n+1 : 1;
    if (n > res)
    res = n;
    previousc = c;
    }
    return res;
    }

    ReplyDelete
  5. public int maxBlock(String str) {
    int max = 0;
    for (int i = 0; i < str.length(); i++) {
    int count = 0;
    for (int j = i; j < str.length(); j++) {
    if (str.charAt(i) == str.charAt(j)) {
    count++;
    } else {
    break;
    }
    }
    if (count > max) max = count;
    }
    return max;
    }

    Simple & Enough!

    ReplyDelete
  6. public int maxBlock(String str) {
    int count = 0;
    int max = 0;
    char previous = ' ';
    if(str.length()<1) return 0;
    for(int i=0;i<str.length();i++){
    if(previous == str.charAt(i)){
    count++;
    if(max<=count){
    max = count;
    }
    } else {
    count = 0;
    }
    previous = str.charAt(i);
    }
    return max+1;
    }

    ReplyDelete
  7. public int maxBlock(String str) {
    int maxCount = 1;
    int count = 0;
    String dummyString = "";
    String compareString = "";
    char compareChar = 'a';

    if(str.equals("")) {
    return count;
    }

    for(int i=0; i < str.length(); i++) {
    compareChar = str.charAt(i);
    dummyString = str.substring(i, i+1);
    count = 1; //reset count to 1

    for(int f = i+2; f <= str.length(); f++) {
    compareString = str.substring(i, f);
    dummyString+=compareChar;

    if(compareString.equals(dummyString)) {
    count++;

    if(maxCount < count) {
    maxCount = count;
    }
    }
    }
    }
    return maxCount;
    }

    ReplyDelete
  8. public int maxBlock(String str) {
    if (str.length() < 2)
    {
    return str.length();
    }
    else
    {
    int count = 1;
    int max = 1;
    for (int i = 0; i < str.length() - 1; i++)
    {
    if (str.charAt(i) == str.charAt(i + 1))
    {
    count++;
    max = Math.max(count, max);
    }
    else
    {
    count = 1;
    }
    }
    return max;
    }
    }

    ReplyDelete
  9. public int maxBlock(String str) {
    int block = 1, max = 0;
    for (int i = 0; i < str.length() - 1; i++) {
    while (i + 1 < str.length() && str.charAt(i + 1) == str.charAt(i)) {
    i++;
    block++;
    }
    max = Math.max(block, max);
    block = 1;
    }
    return max;
    }

    ReplyDelete
  10. public int maxBlock(String str) {
    int count=1;
    int max=1;
    if(str.length()<2)return str.length();
    for(int i=0 ; i<str.length();i++){
    if(i<str.length()-1&& str.charAt(i)==str.charAt(i+1) ){count++;
    max=Math.max(max,count);}
    else {count=1;}
    }
    return max;
    }

    ReplyDelete
  11. public int maxBlock(String str) {
    if(str.length() == 0){
    return 0;
    }
    int max = 1;
    int counter = 1;
    for (int i = 0; i < str.length() - 1; i++){
    if(str.charAt(i) == str.charAt(i + 1)){
    counter++;
    if(counter > max){
    max = counter;
    }
    }else{
    counter = 1;
    }
    }
    return max;
    }

    ReplyDelete
  12. public int maxBlock(String str) {
    int beginner = 0;
    int h = 0;
    int counter = 0;
    int[] index = new int[str.length()];
    for (int i = 0; i < str.length(); i++) {
    char ch = str.charAt(beginner);
    if (ch == str.charAt(i)) {
    counter++;
    } else {
    beginner = i;
    index[h] = counter;
    h++;
    i--;
    counter = 0;
    }
    index[h] = counter;
    }
    if (str.length() <= 0) return 0;
    int max = index[0];
    for (int i = 0; i < index.length; i++) {
    max = Math.max(max, index[i]);
    }
    return max;
    }

    ReplyDelete
  13. public int maxBlock(String str) {
    if(str.length() == 0) return 0;
    int count = 1;
    int s = 0;
    for(int i = 0; is) s = count;
    count = 1;
    }
    }
    if(count>s) s = count;
    return s;
    }

    ReplyDelete
    Replies
    1. public int maxBlock(String str) {
      if(str.length() == 0) return 0;
      int count = 1;
      int s = 0;
      for(int i = 0; is) s = count;
      count = 1;
      }
      }
      if(count>s) s = count;
      return s;
      }

      Delete