leetcode:Word Search II

23 篇文章 0 订阅

题目:

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

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

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

[
  ['o','a','a','n'],
  ['e','t','a','e'],
  ['i','h','k','r'],
  ['i','f','l','v']
]
Return  ["eat","oath"] .

Note:
You may assume that all inputs are consist of lowercase letters a-z.

题意:从一组字符串中找出能在字符表中连得起来的字符串,返回这些字符串。

思路:Word Search中只需要判断一个字符串是否在字符表中连得起来,首先想到的方法是在这基础上对每个字符串环判断,不出所料,超时了。

可先对所有字符串构建字典树,Implement Trie (Prefix Tree)这一题的代码可直接拿来当构建字典树用(适当修改),DFS搜索到某个点不在字典树便结束这次DFS,搜索到某个点为某个字符串的标记结束点时把该字符串加入候选集中。

参考代码:

class TrieNode {
public:
	TrieNode *ch[26];
	bool iskey;
	// Initialize your data structure here.
	TrieNode() :iskey(false) {
		for (auto &a : ch) {
			a = NULL;
		}
	}
};

class Trie {
public:
	Trie() {
		root = new TrieNode();
	}

	// Inserts a word into the trie.
	void insert(string word) {
		TrieNode *tmp = root;
		for (auto i : word) {
			int a = i - 'a';
			if (!tmp->ch[a]) {
				tmp->ch[a] = new TrieNode();
			}
			tmp = tmp->ch[a];
		}
		tmp->iskey = true;
	}

	// Returns if the word is in the trie.
	bool search(string word) {
		TrieNode *tmp = root;
		for (auto i : word) {
			int a = i - 'a';
			if (!tmp->ch[a]) {
				return false;
			}
			tmp = tmp->ch[a];
		}
		if (tmp->iskey) {
			tmp->iskey = false;
			return true;
		}
		return false;
	}

	// Returns if there is any word in the trie
	// that starts with the given prefix.
	bool startsWith(string prefix) {
		TrieNode *tmp = root;
		for (auto i : prefix) {
			int a = i - 'a';
			if (!tmp->ch[a]) {
				return false;
			}
			tmp = tmp->ch[a];
		}
		return true;
	}

private:
	TrieNode* root;
};

// Your Trie object will be instantiated and called as such:
// Trie trie;
// trie.insert("somestring");
// trie.search("key");
class Solution {
public:
	vector<string> res;
	vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
		Trie *root = new Trie();
		for (auto a : words) 
			root->insert(a);
		int m = board.size();
		int n = board[0].size();
		vector<vector<bool>> visited(m, vector<bool>(n, false));
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				dfs(board, visited, i, j, "",root);
			}
		}
		return res;
	}
	void dfs(vector<vector<char>>& board, vector<vector<bool>>& visited,int x,int y,string str,Trie *root) {
		int m = board.size();
		int n = board[0].size();
		if (x < 0 || x >= m || y < 0 || y >= n) return;
		if (visited[x][y]) return;
		str += board[x][y];
		if (!root->startsWith(str)) return;
		if (root->search(str)) {
			res.push_back(str);
		}
		visited[x][y] = true;
		dfs(board, visited, x + 1, y, str, root);
		dfs(board, visited, x - 1, y, str, root);
		dfs(board, visited, x, y + 1, str, root);
		dfs(board, visited, x, y - 1, str, root);
		visited[x][y] = false;
	}
};





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值