Today I have solved the problem in javascript
You are given a string formed by concatenating several words corresponding to the integers zero through nine and then anagramming.
For example, the input could be 'niesevehrtfeev', which is an anagram of 'threefiveseven'. Note that there can be multiple instances of each integer.
Given this string, return the original integers in sorted order.
const input = 'niesevehrtfeev';
const output = originalIntegers(input);
function originalIntegers(inputString) {
const numberMap = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
};
const countMap = {};for (const char of inputString) {
countMap[char] = (countMap[char] || 0) + 1;
}
const result = [];
for (const number in numberMap) {
const numberLetters = number.split('');
const minCount = Math.min(...numberLetters.map(letter => countMap[letter] || 0));
for (let i = 0; i < minCount; i++) {
result.push(numberMap[number]);
}
}
result.sort((a, b) => a - b);
return result;
}
console.log(output);
Output:
[ 3, 5, 7, 9 ]
Comments
Post a Comment