Bomb Enemy - Problem

You are given an m x n matrix grid where each cell contains one of the following:

  • 'W' - represents a wall
  • 'E' - represents an enemy
  • '0' - represents an empty cell

You want to place a bomb in an empty cell that will kill the maximum number of enemies. The bomb kills all enemies in the same row and column from the bomb's position until it hits a wall, since walls are too strong to be destroyed.

Return the maximum number of enemies you can kill with one bomb.

Input & Output

Example 1 — Basic Grid
$ Input: grid = [["0","E","0","0"],["E","0","W","E"],["0","E","0","0"]]
Output: 3
💡 Note: Placing bomb at (1,1) kills 3 enemies: one to the left in same row, one above and one below in same column
Example 2 — Wall Blocking
$ Input: grid = [["0","0","0"],["E","W","E"],["0","0","0"]]
Output: 1
💡 Note: Wall blocks the bomb explosion, so maximum enemies killed is 1
Example 3 — No Empty Cells
$ Input: grid = [["W","E"],["E","W"]]
Output: 0
💡 Note: No empty cells available to place a bomb, so return 0

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 500
  • grid[i][j] is either '0', 'E', or 'W'

Visualization

Tap to expand
Bomb Enemy Problem Overview0E00E💣WE0E00Input Grid💥Explosion PatternResult3Bomb at (1,1) kills 1 enemy left + 1 enemy up + 1 enemy down = 3 totalWall at (1,2) blocks explosion to the right
Understanding the Visualization
1
Input Grid
Matrix with walls (W), enemies (E), and empty cells (0)
2
Bomb Explosion
Bomb kills enemies in same row and column until hitting walls
3
Maximum Result
Return the highest enemy count possible
Key Takeaway
🎯 Key Insight: Walls create independent segments - pre-compute enemy counts to avoid redundant calculations
Asked in
Google 45 Facebook 38 Amazon 32
32.5K Views
Medium Frequency
~25 min Avg. Time
876 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen