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
Crack Alphabets fight problem in JavaScript
Problem
Consider a situation where armies of two teams of alphabets are fighting each other. Each soldier has a specific weight based on their letter:
Team A (Positive Weights)
| Soldier | Weight |
|---|---|
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
Team B (Negative Weights)
| Soldier | Weight |
|---|---|
| W | -1 |
| X | -2 |
| Y | -3 |
| Z | -4 |
Bombs are represented by '!' and they kill soldiers placed adjacent to them. For example:
- 'A!BC' results in 'C' (A and B are killed by the bomb)
- '!!CC!!' results in '' (all C's are killed by adjacent bombs)
Our function determines which team wins after all bombs explode, or if it's a tie.
Algorithm
The solution works by:
- Filtering out soldiers that are adjacent to bombs
- Calculating the total weight of surviving soldiers
- Determining the winner based on the final score
Example
const str = '!WX!YZ!DC!BA!';
const stringFight = (str) => {
const map = {
'D': 4, 'C': 3, 'B': 2, 'A': 1,
'Z': -4, 'Y': -3, 'X': -2, 'W': -1
};
const survivors = [];
const arr = str.split('');
// Check each character to see if it survives the bombs
for(let i = 0; i < str.length; i++){
// Soldier survives if not adjacent to any bomb
if(arr[i-1] !== '!' && arr[i] !== '!' && arr[i+1] !== '!'){
survivors.push(arr[i]);
}
}
// Calculate total weight of survivors
const totalWeight = survivors.reduce((sum, soldier) => {
return sum + (map[soldier] ? map[soldier] : 0);
}, 0);
// Determine winner
if(totalWeight < 0){
return 'Team B';
} else if(totalWeight > 0){
return 'Team A';
} else {
return 'Tie';
}
};
console.log(stringFight(str));
Tie
Step-by-Step Breakdown
Let's trace through the example '!WX!YZ!DC!BA!':
const str = '!WX!YZ!DC!BA!';
console.log("Original string:", str);
// Check each position for survivors
const positions = str.split('').map((char, i) => {
const prev = str[i-1];
const curr = str[i];
const next = str[i+1];
const survives = prev !== '!' && curr !== '!' && next !== '!';
return { position: i, char, survives };
});
console.log("Position analysis:");
positions.forEach(p => {
console.log(`Position ${p.position}: '${p.char}' - ${p.survives ? 'Survives' : 'Dies'}`);
});
// Only survivors contribute to final score
const survivors = positions.filter(p => p.survives).map(p => p.char);
console.log("Survivors:", survivors);
console.log("Final score: 0 (no survivors) = Tie");
Original string: !WX!YZ!DC!BA! Position analysis: Position 0: '!' - Dies Position 1: 'W' - Dies Position 2: 'X' - Dies Position 3: '!' - Dies Position 4: 'Y' - Dies Position 5: 'Z' - Dies Position 6: '!' - Dies Position 7: 'D' - Dies Position 8: 'C' - Dies Position 9: '!' - Dies Position 10: 'B' - Dies Position 11: 'A' - Dies Position 12: '!' - Dies Survivors: [] Final score: 0 (no survivors) = Tie
Conclusion
This problem demonstrates array filtering and conditional logic in JavaScript. The key insight is that any character adjacent to a bomb ('!') gets eliminated, and the winner is determined by the total weight of surviving soldiers.
Advertisements
