[Leedcode 212] Word Search II

Source: Internet
Author: User

Given a 2D board and a list of words from the dictionary, find all words in the board.

Each word must is constructed from letters of sequentially adjacent cell, where "adjacent" cells is those horizontally or Vertically neighboring. The same letter cell is used more than once in a word.

For example,
Given words = and ["oath","pea","eat","rain"] board =

[  [' O ', ' a ', ' a ', ' n '],  [' e ', ' t ', ' a ', ' e '],  [' I ', ' h ', ' K ', ' R '],  [' I ', ' f ', ' l ', ' V ']]

Return ["eat","oath"] .

class Solution {public:    vector<string> findwords (vector<vector<Char >>& Board, vector<string>& words) {      /*   just saw a word before the topic thought Search, so immediately think of the violence of searching every word, but unfortunately with the same as expected time-out, looked at hint, referred to the dictionary tree. So first use the words given to build the dictionary tree, and then in the DFS search the dictionary tree of words, so as to avoid a large number of repeated comparisons. In addition, if the search path is not a dictionary tree prefix, you can directly prune back. The following is the AC code. Because to avoid repetition, I first use a set to save the result and then dump it into result. */    }};

[Leedcode 212] Word Search II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.