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; }
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteint 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;
}
String sub = "";
ReplyDeleteint 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;
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteint 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;
}
for python makers :D :
ReplyDeletedef 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
fixed
Deletedef 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