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
Forming and matching strings of an array based on a random string in JavaScript
In JavaScript, we can check if strings from an array can be formed using characters from a given string. This involves counting character frequencies and matching them against each array element.
Problem Statement
Given an array of strings and a random string, find the first string from the array that can be completely formed using the characters available in the random string.
const arr = ['Dinesh', 'Mahesh', 'Rohit', 'Kamal', 'Jatin Sapru', 'Jai'];
const str = 'lsoaakjm';
console.log("Array:", arr);
console.log("Available characters:", str);
Array: [ 'Dinesh', 'Mahesh', 'Rohit', 'Kamal', 'Jatin Sapru', 'Jai' ] Available characters: lsoaakjm
Solution Approach
We'll use a character frequency map to track available characters and check if each string can be formed:
const initialise = (str = '', map) => {
for(let i = 0; i {
for(key in map){
delete map[key];
}
};
const checkForFormation = (arr = [], str = '') => {
const map = {};
for(let i = 0; i
Kamal
How It Works
The algorithm works by:
- Creating a frequency map of available characters from the input string
- For each array element, checking if all its characters can be "consumed" from the map
- Returning the first matching string or an empty string if none match
Step-by-Step Analysis
// Let's trace through why "Kamal" works
const str = 'lsoaakjm';
const target = 'kamal';
// Character frequency in str: l:1, s:1, o:1, a:2, k:1, j:1, m:1
// Characters needed for "kamal": k:1, a:2, m:1, l:1
console.log("Available characters:", str.split('').sort().join(''));
console.log("Kamal needs:", target.split('').sort().join(''));
console.log("Can form 'Kamal':", target.split('').every(char => str.includes(char)));
Available characters: aajklmos
Kamal needs: aakml
Can form 'Kamal': true
Alternative Simplified Solution
const canFormString = (target, source) => {
const sourceMap = {};
// Count characters in source
for (let char of source.toLowerCase()) {
sourceMap[char] = (sourceMap[char] || 0) + 1;
}
// Check if target can be formed
for (let char of target.toLowerCase()) {
if (char === ' ') continue; // Skip spaces
if (!sourceMap[char] || sourceMap[char] === 0) {
return false;
}
sourceMap[char]--;
}
return true;
};
const findFormableString = (arr, str) => {
return arr.find(name => canFormString(name, str)) || '';
};
const names = ['Dinesh', 'Mahesh', 'Rohit', 'Kamal', 'Jatin Sapru', 'Jai'];
const characters = 'lsoaakjm';
console.log("First formable name:", findFormableString(names, characters));
First formable name: Kamal
Conclusion
This pattern is useful for word games, anagram checkers, and string matching algorithms. The key is using character frequency maps to efficiently track available characters and validate string formation.
Advertisements
