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:
01 | public int maxBlock(String str) { |
02 | int len = str.length(); |
03 | int count = 0 ; |
04 | int tmpcount = 1 ; |
05 | |
06 | if (len == 0 ) |
07 | return 0 ; |
08 | |
09 | for ( int i = 0 ; i < len; i++) { |
10 | if (i < len- 1 && str.charAt(i) == str.charAt(i+ 1 )) |
11 | tmpcount++; |
12 | else |
13 | tmpcount = 1 ; |
14 | |
15 | if (tmpcount > count) |
16 | count = tmpcount; |
17 | } |
18 | return count; |
19 | } |
Here's mine.
ReplyDeletepublic 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;
}
Really Works! Simply Superb!
ReplyDeleteI 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;
}
public int maxBlock(String str) {
ReplyDeleteif(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;
}
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteint 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!
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteif (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;
}
}
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteif(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;
}
public int maxBlock(String str) {
ReplyDeleteint 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;
}
public int maxBlock(String str) {
ReplyDeleteif(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;
}
public int maxBlock(String str) {
Deleteif(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;
}