Optimal Fixed | function foo(str) {
counts = [];
maxCh = str.charAt(0);
counts[maxCh] = 1;
for (i = 1; i < str.length; i++) {
ch = str.charAt(i);
if (ch == ' ')
continue;
if (!counts[ch]) {
counts[ch] = 0;
}
if (ch == 'o')
counts[ch] += 0.5;
else
counts[ch]++;
if (counts[ch] > counts[maxCh]) {
maxCh = ch;
}
}
return Math.ceil(counts[maxCh]);
}
if (foo('coffee kebab') != 3) {
throw new Error('Test failed for "coffee kebab". Expected 3, returned ' + foo('coffee kebab'));
}
if (foo('book') != 1) {
throw new Error('Test failed for "book". Expected 1, returned ' + foo('book'));
}
if (foo('ffacebook') != 2) {
throw new Error('Test failed for "ffacebook". Expected 2, returned ' + foo('ffacebook'));
}
if (foo('fa ce bo ok') != 1) {
throw new Error('Test failed for "fa ce bo ok". Expected 1, returned ' + foo('fa ce bo ok'));
}
| ready |
Original - optimal | function foo(str) {
counts = [];
maxCh = str.charAt(0);
counts[maxCh] = 1;
for (i = 1; i < str.length; i++) {
ch = str.charAt(i);
if (!counts[ch]) {
counts[ch] = 0;
}
if (++counts[ch] > counts[maxCh]) {
maxCh = ch;
}
}
return counts[maxCh];
}
if (foo('coffee kebab') != 3) {
throw new Error('Test failed for "coffee kebab". Expected 3, returned ' + foo('coffee kebab'));
}
if (foo('book') != 1) {
throw new Error('Test failed for "book". Expected 1, returned ' + foo('book'));
}
if (foo('ffacebook') != 2) {
throw new Error('Test failed for "ffacebook". Expected 2, returned ' + foo('ffacebook'));
}
if (foo('fa ce bo ok') != 1) {
throw new Error('Test failed for "fa ce bo ok". Expected 1, returned ' + foo('fa ce bo ok'));
}
| ready |
Original - Better optimal | function foo(str) {
counts = [0,0,0,0,0,0,0];
max = 0;
idx = 0;
for (i = 1; i < str.length; i++) {
ch = str.charAt(i);
switch (ch) {
case 'f': idx = 0; break;
case 'a': idx = 1; break;
case 'c': idx = 2; break;
case 'e': idx = 3; break;
case 'b': idx = 4; break;
case 'o': idx = 5; break;
case 'k': idx = 6; break;
default: idx=0;
}
if (ch == 'o') counts[idx] += 0.5;
else counts[idx]++;
if (counts[idx] > max) {
max = Math.ceil(counts[idx]);
}
}
return max;
}
if (foo('coffee kebab') != 3) {
throw new Error('Test failed for "coffee kebab". Expected 3, returned ' + foo('coffee kebab'));
}
if (foo('book') != 1) {
throw new Error('Test failed for "book". Expected 1, returned ' + foo('book'));
}
if (foo('ffacebook') != 2) {
throw new Error('Test failed for "ffacebook". Expected 2, returned ' + foo('ffacebook'));
}
if (foo('fa ce bo ok') != 1) {
throw new Error('Test failed for "fa ce bo ok". Expected 1, returned ' + foo('fa ce bo ok'));
}
| ready |