Count Phrase in 2D Array
MediumP3Coding
grid is a matrix of lowercase letters. Count how many times the word saba appears in it when read in a straight line of four cells in any of these four directions: left to right, top to bottom, down-right diagonally, and up-right diagonally.
The word is not read backwards (right to left, bottom to top and so on). Occurrences may overlap and share cells.
Example 1
- Input
- grid = [["s","a","b","a"]]
- Output
- 1
Example 2
- Input
- grid = [["s"],["a"],["b"],["a"]]
- Output
- 1
Example 3
- Input
- grid = [["s","x","x","x"],["x","a","x","x"],["x","x","b","x"],["x","x","x","a"]]
- Output
- 1
Constraints
- 1 <= rows, cols <= 100
- grid contains lowercase English letters.
- All rows have the same length.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.