public static void main(String[] args) {
    Trie t = new Trie();
    t.insert("are");
    t.insert("area");
    t.insert("base");
    t.insert("cater");
    t.insert("cat");
    t.insert("basement");

    System.out.println(t.searchPrefix("cater"));
  }
Ejemplo n.º 2
0
 public List<List<Integer>> palindromePairs(String[] words) {
   List<List<Integer>> ans = new LinkedList<List<Integer>>();
   Trie tree = new Trie();
   for (int i = 0; i < words.length; i++) tree.insert(words[i], i);
   for (int i = 0; i < words.length; i++) tree.searchPalindromePairs(ans, words[i], i);
   return ans;
 }
Ejemplo n.º 3
0
 public void loadKeys(ArrayList<String> keys) {
   for (int i = 0; i < keys.size(); i++) {
     // System.out.println("Inserting " + keys.get(i));
     insert(keys.get(i));
   }
   return;
 }
Ejemplo n.º 4
0
  public List<String> findWords(char[][] board, String[] words) {

    if (board == null || words == null) {
      return new ArrayList<>();
    }

    Trie trie = new Trie();

    for (String word : words) {
      trie.insert(word);
    }

    Set<String> wordsList = new HashSet<>();

    for (int i = 0; i < board.length; i++) {
      for (int j = 0; j < board[0].length; j++) {
        StringBuilder word = new StringBuilder();
        boolean[][] used = new boolean[board.length][board[0].length];
        word.append(board[i][j]);
        search(used, board, word, i, j, trie, wordsList);
      }
    }

    return new ArrayList<>(wordsList);
  }
Ejemplo n.º 5
0
 public List<String> findWords(char[][] board, String[] words) {
   List<String> res = new ArrayList<String>();
   Trie trie = new Trie();
   for (String word : words) {
     trie.insert(word);
   }
   boolean[][] visited = new boolean[board.length][board[0].length];
   for (int i = 0; i < board.length; i++) {
     for (int j = 0; j < board[0].length; j++) {
       searchHelper(board, visited, res, trie, "", i, j);
     }
   }
   return res;
 }
Ejemplo n.º 6
0
    public List<String> findWords(char[][] board, String[] words) {

      Trie trie = new Trie();
      for (String word : words) {
        trie.insert(word);
      }

      int m = board.length;
      int n = board[0].length;

      boolean[][] visited = new boolean[m][n];

      for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
          dfs(board, visited, "", i, j, trie);
        }
      }

      return new ArrayList<String>(result);
    }