Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. public class Words {
  2.  
  3. public int countWords(char[][] board) {
  4. int count = 0;
  5.  
  6. int rows = board.length;
  7. int columns = board[0].length;
  8.  
  9. for (int i = 0; i < rows; i++) {
  10. for (int j = 0; j < columns; j++) {
  11. int[][] visited = new int[rows][columns];
  12. count = checkWord(i, j, visited, board, new StringBuilder());
  13. }
  14. }
  15.  
  16. return count;
  17. }
  18.  
  19.  
  20. private int checkWord(int row, int column, int[][] visited, char[][] board, StringBuilder prefix) {
  21. int count = 0;
  22. if (row < 0 || column < 0 || row >= board.length || column >= board[0].length) {
  23. return count;
  24. }
  25.  
  26. if (visited[row][column] == 1) {
  27. return count;
  28. }
  29.  
  30. prefix.append(board[row][column]);
  31. visited[row][column] = 1;
  32.  
  33. if (new Dictionary().isWord(prefix.toString())) {
  34. count++;
  35. }
  36.  
  37. for (int i = row-1; i <= row+1; i++) {
  38. for (int j = column-1; j < column-1; j++) {
  39. count += checkWord(i, j, visited, board, prefix);
  40. }
  41. }
  42.  
  43. return count;
  44. }
  45.  
  46. class Dictionary {
  47. Dictionary() { }
  48.  
  49. boolean isWord(String word) {
  50. return true;
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement