Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Words {
- public int countWords(char[][] board) {
- int count = 0;
- int rows = board.length;
- int columns = board[0].length;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- int[][] visited = new int[rows][columns];
- count = checkWord(i, j, visited, board, new StringBuilder());
- }
- }
- return count;
- }
- private int checkWord(int row, int column, int[][] visited, char[][] board, StringBuilder prefix) {
- int count = 0;
- if (row < 0 || column < 0 || row >= board.length || column >= board[0].length) {
- return count;
- }
- if (visited[row][column] == 1) {
- return count;
- }
- prefix.append(board[row][column]);
- visited[row][column] = 1;
- if (new Dictionary().isWord(prefix.toString())) {
- count++;
- }
- for (int i = row-1; i <= row+1; i++) {
- for (int j = column-1; j < column-1; j++) {
- count += checkWord(i, j, visited, board, prefix);
- }
- }
- return count;
- }
- class Dictionary {
- Dictionary() { }
- boolean isWord(String word) {
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement