Problem · Matrix
Can Fit the Word
Learn this problemProblem statement
You are given a rectangular character grid containing blocked cells #, empty cells _, and English letters, plus a string word.
A slot is a maximal contiguous run of non-blocked cells in one row or one column. The word fits when a slot has exactly the same length as the word and, reading left-to-right or top-to-bottom, every slot cell is either _ or already equals the corresponding word character. Empty cells may be filled with the required letters.
Return 1 if the word fits in at least one slot; otherwise return 0.
Function
canFitTheWord(grid: char[][], word: String) → intExamples
Example 1
grid = [['#', '#', '#', '#'], ['L', '_', '#', '#'], ['A', 'L', 'A', '_'], ['#', 'X', '#', '#']]word = "ALA"return = 0The row containing ALA_ is a four-cell slot, so placing the three-letter word would leave one empty cell. No vertical slot matches.
Example 2
grid = [['#', '#', '#', '#'], ['L', '_', '#', '#'], ['A', 'L', 'A', 'N'], ['#', 'X', '#', '#']]word = "ALAN"return = 1The third row is one complete four-cell slot and exactly spells ALAN.
Constraints
gridis non-empty and rectangular.- Every grid cell is
#,_, or an English letter. wordis non-empty and contains English letters.
More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026