sweet1cris

Untitled

Dec 25th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. class Solution {
  2.     final int[][] DIRS = {{-1, 0},  {1, 0}, {0, -1}, {0, 1}};
  3.     public List<String> findWords(char[][] board, String[] words) {
  4.         List<String> result = new ArrayList<>();
  5.         if (board == null || board.length == 0 || board[0].length == 0 || words == null || words.length == 0) {
  6.             // throw new illegalargumentexception("invalid input");
  7.             return result;
  8.         }        
  9.         Trie trie = new Trie();
  10.         for (String word : words) {
  11.             trie.insert(word);
  12.         }
  13.         int rows = board.length;
  14.         int cols = board[0].length;
  15.         StringBuilder sb = new StringBuilder();
  16.         for (int i = 0; i < rows; i++) {
  17.             for (int j = 0; j < cols; j++) {
  18.                 dfs(board, i, j, trie.root, sb, result, rows, cols);
  19.             }
  20.         }
  21.         return result;
  22.     }
  23.     private void dfs(char[][] board, int x, int y, TrieNode cur, StringBuilder sb, List<String> result, int rows, int cols) {
  24.         if (x < 0 || x >= rows || y < 0 || y >= cols) {
  25.             return;
  26.         }
  27.         char ch = board[x][y];
  28.         if (ch == '#' || cur.children.get(ch) == null) {
  29.             return;
  30.         }
  31.         board[x][y] = '#';
  32.         TrieNode next = cur.children.get(ch);
  33.         sb.append(ch);
  34.         if (next.isEnd) {
  35.             result.add(sb.toString());
  36.             next.isEnd = false;
  37.         }
  38.         for (int[] dir : DIRS) {
  39.             int neiX = x + dir[0];
  40.             int neiY = y + dir[1];
  41.             dfs(board, neiX, neiY, next, sb, result, rows, cols);
  42.         }
  43.         sb.deleteCharAt(sb.length() - 1);
  44.         board[x][y] = ch;
  45.     }
  46. }
  47. class Trie {
  48.     TrieNode root;    
  49.     public Trie() {                    /** Initialize your data structure here. */
  50.         root = new TrieNode();
  51.     }      
  52.     public void insert(String word) {   /** Inserts a word into the trie. */
  53.         if (word == null || word.length() == 0) {
  54.             return;
  55.         }
  56.         TrieNode cur = root;
  57.         for (int i = 0; i < word.length(); i++) {
  58.             TrieNode next = cur.children.get(word.charAt(i));
  59.             if (next == null) {
  60.                 next = new TrieNode();
  61.                 cur.children.put(word.charAt(i), next);
  62.             }
  63.             cur = next;
  64.         }
  65.         cur.isEnd = true;
  66.     }      
  67.     public boolean search(String word) { /** Returns if the word is in the trie. */
  68.         if (word == null || word.length() == 0) {
  69.             return false;
  70.         }
  71.         TrieNode cur = root;
  72.         for (int i = 0; i < word.length(); i++) {
  73.             TrieNode next = cur.children.get(word.charAt(i));
  74.             if (next == null) {
  75.                 return false;
  76.             }
  77.             cur = next;
  78.         }
  79.         return cur.isEnd;
  80.     }  
  81.     /** Returns if there is any word in the trie that starts with the given prefix. */
  82.     public boolean startsWith(String prefix) {
  83.         if (prefix == null || prefix.length() == 0) {
  84.             return true;
  85.         }
  86.         TrieNode cur = root;
  87.         for (int i = 0; i < prefix.length(); i++) {
  88.             TrieNode next = cur.children.get(prefix.charAt(i));
  89.             if (next == null) {
  90.                 return false;
  91.             }
  92.             cur = next;
  93.         }
  94.         return true;
  95.     }
  96. }
  97. class TrieNode {
  98.     Map<Character, TrieNode> children = new HashMap<>();
  99.     boolean isEnd;
  100.     public TrieNode() {}
  101. }
Advertisement
Add Comment
Please, Sign In to add comment