Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Preparing numbers from jumbled number names in JavaScript
Problem
Suppose we have the following jumbled number name string:
const str = 'TOWNE';
If we rearrange this string, we can find two number names in it: 2 (TWO) and 1 (ONE).
Therefore, we expect an output of 21.
We need to write a JavaScript function that takes in one such string and returns the numbers present in the string arranged in ascending order.
Approach
The solution involves:
- Creating a mapping of number words to their numeric values
- Generating permutations to check if number words can be formed
- Finding matches and sorting them by their position in the original string
- Combining the found numbers into a single result
Example
const str = 'TOWNE';
const findNumber = (str = '') => {
function stringPermutations(str) {
const res = [];
if (str.length == 1) return [str];
if (str.length == 2) return [str, str[1] + str[0]];
str.split('').forEach((chr, ind, arr) => {
let sub = [].concat(arr);
sub.splice(ind, 1);
stringPermutations(sub.join('')).forEach(function (perm) {
res.push(chr + perm);
});
});
return res;
}
const legend = {
'ONE': 1, 'TWO': 2, 'THREE': 3, 'FOUR': 4,
'FIVE': 5, 'SIX': 6, 'SEVEN': 7, 'EIGHT': 8,
'NINE': 9, 'ZERO': 0
};
const keys = Object.keys(legend);
const res = {};
const resArr = [];
let result = '';
keys.forEach(key => {
const match = stringPermutations(key).find(el =>
el.split('').every(char => str.includes(char))
);
if (match) {
const index = str.indexOf(match[0]);
if (!res.hasOwnProperty(key)) {
res[key] = [index];
} else if (!res[key].includes(index)) {
res[key].push(index);
}
}
});
Object.keys(res).forEach(word => {
resArr.push([word, ...res[word]]);
});
resArr.sort((a, b) => a[1] - b[1]);
resArr.forEach(sub => {
result = result + String(legend[sub[0]]).repeat(sub.length - 1);
});
return +result;
};
console.log(findNumber(str));
Output
21
How It Works
The algorithm works by:
- Permutation Generation: Creates all possible arrangements of each number word
- Character Matching: Checks if all characters of a number word exist in the input string
- Position Tracking: Records the position where each number word starts
- Sorting: Arranges found numbers by their position in the original string
- Result Formation: Combines the numbers to form the final result
Testing with Different Inputs
// Test with more complex examples
console.log(findNumber('ENONOWT')); // Contains ONE, TWO
console.log(findNumber('XISEERHT')); // Contains SIX, THREE
console.log(findNumber('ENOWTORUOF')); // Contains ONE, TWO, FOUR
21 36 124
Conclusion
This solution uses string permutations and character matching to extract number words from jumbled strings. The algorithm handles multiple numbers and returns them in the order they appear in the original string.
Advertisements
