1. 程式人生 > >79.Word Search&&212.Word Search||

79.Word Search&&212.Word Search||

class Solution {
    
    
    boolean[][] visited;
    public boolean exist(char[][] board, String word) {
        visited = new boolean[board.length][board[0].length];
        for (int i = 0; i < board.length; i++){
            for (int j = 0; j < board[i].length; j++){
                if (word.charAt(0) == board[i][j] && check(board, word, 0, i, j)) return true;
            }
        }
        
        
        return false;
    }
    
    private boolean check(char[][] board, String word, int index, int i, int j){
        if (index == word.length()) return true;
        if (i < 0 || i >= board.length || j < 0 || j >= board[i].length || visited[i][j] || word.charAt(index) != board[i][j]) return false;
        
        visited[i][j] = true;
        if (check(board, word, index + 1, i + 1, j) || 
            check(board, word, index + 1, i - 1, j) || 
            check(board, word, index + 1, i, j + 1) ||
            check(board, word, index + 1, i, j -1)) return true;
        
        visited[i][j] = false;
        
        return false;
    
    }
}
public class Solution {
    static boolean[][] visited;
    public boolean exist(char[][] board, String word) {
        visited = new boolean[board.length][board[0].length];
        
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[i].length; j++){
                if((word.charAt(0) == board[i][j]) && search(board, word, i, j, 0)){
                    return true;
                }
            }
        }
        
        return false;
    }
    
    private boolean search(char[][]board, String word, int i, int j, int index){
        if(index == word.length()){
            return true;
        }
        
        if(i >= board.length || i < 0 || j >= board[i].length || j < 0 || board[i][j] != word.charAt(index) || visited[i][j]){
            return false;
        }
        
        visited[i][j] = true;
        if(search(board, word, i-1, j, index+1) || 
           search(board, word, i+1, j, index+1) ||
           search(board, word, i, j-1, index+1) || 
           search(board, word, i, j+1, index+1)){
            return true;
        }
        
        visited[i][j] = false;
        return false;
    }
}
public boolean exist(char[][] board, String word) {
    char[] w = word.toCharArray();
    for (int y=0; y<board.length; y++) {
    	for (int x=0; x<board[y].length; x++) {
    		if (exist(board, y, x, w, 0)) return true;
    	}
    }
    return false;
}

private boolean exist(char[][] board, int y, int x, char[] word, int i) {
	if (i == word.length) return true;
	if (y<0 || x<0 || y == board.length || x == board[y].length) return false;
	if (board[y][x] != word[i]) return false;
	board[y][x] ^= 256;
	boolean exist = exist(board, y, x+1, word, i+1)
		|| exist(board, y, x-1, word, i+1)
		|| exist(board, y+1, x, word, i+1)
		|| exist(board, y-1, x, word, i+1);
	board[y][x] ^= 256;
	return exist;
}
class Solution {
    int[] dx = {1, -1, 0, 0};
    int[] dy = {0, 0, 1, -1};
    public boolean exist(char[][] board, String word) {   
        if(board == null || board.length == 0 || word == null)
            return false;
        int[][] visited = new int[board.length][board[0].length];
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[0].length; j++){
                if(board[i][j] == word.charAt(0) && helper(board, visited, i, j, word, 0))
                    return true;
            }
        }
        return false;
    }
        
        private boolean helper(char[][] board, int[][] visited, int i, int j, String word, int idx){
            if(idx == word.length())
                return true;
            if(i>=0 && i<board.length && j>=0 && j<board[0].length && visited[i][j] != 1 && word.charAt(idx) == board[i][j]){
                visited[i][j]=1;
                for(int diff =0; diff<dx.length ; diff++){
                    if(helper(board, visited, i+dx[diff], j+dy[diff], word, idx+1)){
                        return true;
                    }
                }
                visited[i][j]=0;
                
            }
            return false;
        }
        
    
} 

 

class Solution {
    class Node{
        boolean isWord;
        String word;
        char val;
        Node[] children;
        public Node(char val){
            isWord = false;
            this.children = new Node[26];
            this.val = val;
        }
    }
    public List<String> findWords(char[][] board, String[] words) {
        Node root = buildTrie(words);
        Set<String> result = new HashSet<String>();
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[0].length; j++){
                boolean[][] visited = new boolean[board.length][board[0].length];
                search(board,visited,i,j,result,root);
            }
        }
        return Arrays.asList(result.toArray(new String[result.size()]));
    }
    
    public void search(char[][] board, boolean[][] visited, int i, int j, Set<String> result, Node root){
        if(i<0 || j<0 || i>=board.length || j>=board[0].length || visited[i][j]) return;
        char ch = board[i][j];
        root = root.children[ch-'a'];
        if(root == null) return;
        visited[i][j] = true;
        if(root.isWord) {
            result.add(root.word);
        }
        search(board,visited,i+1,j,result,root);
        search(board,visited,i-1,j,result,root);
        search(board,visited,i,j+1,result,root);
        search(board,visited,i,j-1,result,root);
        visited[i][j] = false;
    }
    
    public Node buildTrie(String[] words){
        Node root = new Node('#');
        Node node = root;
        for(int i=0; i<words.length; i++){
            String word = words[i];
            root = node;
            for(int j=0; j<word.length(); j++){
                char ch = word.charAt(j);
                if(root.children[ch-'a'] == null){
                    root.children[ch-'a'] = new Node(ch);
                }
                root = root.children[ch-'a'];
            }
            root.isWord = true;
            root.word = words[i];
        }
        return node;
    }
    
    
}
public List<String> findWords(char[][] board, String[] words) {
    List<String> res = new ArrayList<>();
    TrieNode root = buildTrie(words);
    for (int i = 0; i < board.length; i++) {
        for (int j = 0; j < board[0].length; j++) {
            dfs (board, i, j, root, res);
        }
    }
    return res;
}

public void dfs(char[][] board, int i, int j, TrieNode p, List<String> res) {
    char c = board[i][j];
    if (c == '#' || p.next[c - 'a'] == null) return;
    p = p.next[c - 'a'];
    if (p.word != null) {   // found one
        res.add(p.word);
        p.word = null;     // de-duplicate
    }

    board[i][j] = '#';
    if (i > 0) dfs(board, i - 1, j ,p, res); 
    if (j > 0) dfs(board, i, j - 1, p, res);
    if (i < board.length - 1) dfs(board, i + 1, j, p, res); 
    if (j < board[0].length - 1) dfs(board, i, j + 1, p, res); 
    board[i][j] = c;
}

public TrieNode buildTrie(String[] words) {
    TrieNode root = new TrieNode();
    for (String w : words) {
        TrieNode p = root;
        for (char c : w.toCharArray()) {
            int i = c - 'a';
            if (p.next[i] == null) p.next[i] = new TrieNode();
            p = p.next[i];
       }
       p.word = w;
    }
    return root;
}

class TrieNode {
    TrieNode[] next = new TrieNode[26];
    String word;
}