Count Battleships by Size
Updated
Problem statement
You are given a rectangular Battleship board as an array of equal-length strings grid:
#is part of a ship..is water.
Each ship is one maximal group of # cells connected vertically or horizontally. Every ship is guaranteed to be a straight horizontal or vertical segment of length 1, 2, or 3. Distinct ships do not touch vertically or horizontally, although they may touch diagonally.
Return an integer array [single, double, triple], where:
singleis the number of length-1ships.doubleis the number of length-2ships.tripleis the number of length-3ships.
Function
countShipTypes(grid: String[]) → int[]Examples
Example 1
grid = ["#..##.","......","###...","......"]return = [1,1,1]The board contains one isolated cell, one horizontal ship of length 2, and one horizontal ship of length 3.
Example 2
grid = ["#.#","..#","..#","...","##."]return = [1,1,1]The board contains a single-cell ship, a horizontal length-2 ship, and a vertical length-3 ship.
Example 3
grid = ["#.#.#","..#.#","....#","##...","....."]return = [1,2,1]There is one length-1 ship, two length-2 ships, and one length-3 ship. Diagonal contact does not merge ships.
Constraints
1 <= grid.length <= 500.1 <= grid[i].length <= 500.- Every row has the same length.
- Every cell is
#or.. - Every maximal orthogonally connected ship is a straight segment of length
1,2, or3.
More Deloitte Online Assessment questions
- Seventh Nearest PalindromeOA · Seen May 2026
- Minimum Deletions for a Non-PalindromeOA · Seen Jul 2025
- Minimum-Cost Solar Rooftop AllocationOA · Seen Jul 2025
- Pair Elements with Even SumsOA · Seen Jul 2025
- Count Divisible PermutationsOA · Seen Jan 2025
- Largest Element in an ArrayOA · Seen Nov 2024
- Install Carbon FiltersOA · Seen Oct 2023
- Longest Substring with Even OccurrencesOA · Seen Oct 2023
- Maximum Sum RectangleOA · Seen Apr 2022
- Annual Bank Balance with Monthly Card FeesOA · Seen Jan 2022
Similar questions at other companies
- Reveal a Minesweeper RegionNuro · PHONE SCREENNuro interview questions
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
- Invert or Blur an ImageOpenAI · OAOpenAI interview questions
- Count Pieces After Othello MovesHudson River Trading · OAHudson River Trading interview questions
- Trapped Characters in a GridAdobe · OAAdobe interview questions
- Simulate a Deterministic Finite AutomatonPoint72 · OAPoint72 interview questions
- Connected GroupsVisa · OAVisa interview questions
- Collect Letters Along a Unique Grid PathApple · OAApple interview questions
- Find Largest House AreaHSBC · OAHSBC interview questions
- Rover MoveIBM · OAIBM interview questions