cached | function computeStringMaxValue(string) {
if (typeof string !== 'string' || string.length < 2 || string > 500) {
return -1;
}
var inputChars = string.toLowerCase().match(/[a-z]/g),
charsUsageObj = {},
sum = 0,
i;
if (!inputChars) {
return 0;
}
for (i = 0; i < inputChars.length; i++) {
if (charsUsageObj.hasOwnProperty(inputChars[i])) {
charsUsageObj[inputChars[i]]++;
} else {
charsUsageObj[inputChars[i]] = 1;
}
}
var letterSorted = Object.keys(charsUsageObj).sort(function _sortFn(a, b) {
if (charsUsageObj[a] < charsUsageObj[b]) {
return -1;
}
if (charsUsageObj[a] > charsUsageObj[b]) {
return 1;
}
return 0;
});
var alphabetOffset = 26 - letterSorted.length;
for (i = 0; i < letterSorted.length; i++) {
sum += (alphabetOffset + i + 1) * charsUsageObj[letterSorted[i]];
}
return sum;
}
computeStringMaxValue('We are looking forward for your entries...');
| ready |
not cached | function computeStringMaxValue(string) {
if (typeof string !== 'string' || string.length < 2 || string > 500) {
return -1;
}
var inputChars = string.toLowerCase(),
alphabet = 'abcdefghijklmnopqrstuvwxyz',
charsUsageObj = {},
sum = 0;
for (var i = 0; i < inputChars.length; i++) {
var char = inputChars[i],
isCharExists = charsUsageObj.hasOwnProperty(char),
isCharInAlphabet = alphabet.indexOf(char) > -1;
if (isCharExists) {
charsUsageObj[char]++;
} else if (isCharInAlphabet) {
charsUsageObj[char] = 1;
alphabet = alphabet.replace(char, '');
}
}
var letterSorted = Object.keys(charsUsageObj).sort(function _sortFn(a, b) {
if (charsUsageObj[a] < charsUsageObj[b]) {
return -1;
}
if (charsUsageObj[a] > charsUsageObj[b]) {
return 1;
}
return 0;
}).join('');
alphabet = alphabet + letterSorted;
inputChars.split('').map(function _createNumArr(char) {
var position = alphabet.indexOf(char);
return (position > -1) ? (position + 1) : 0;
}).map(function _sumFn(value) {
sum += value;
});
return sum;
}
computeStringMaxValue('We are looking forward for your entries...');
| ready |