212. Word Search II

题目

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.

我的想法

感觉可以用bfs做。外层遍历每一个点做为起点,并加入stack。每次从stack中pop出一个点,判断其周边有没有可用的点,如果有则加入stack。
但是越写越觉得不对劲,bfs得到的会有很多个中间值,全都要存一遍

class Solution {
    public List<String> findWords(char[][] board, String[] words) {
        HashSet<String> wordsSet = new HashSet<>(words);
        List<String> res = new ArrayList<>();
        int rows = board.length;
        int cols = board[0].length;
        int dircs = {{-1, 0}, {+1, 0}, {0, +1}, {0, -1}};
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {

            }
        }
    }
    private boolean isContain(String[] words, char[][] board, 
                              int si, int sj, HashSet<String> wordsSet, 
                              List<String> res) {
        char start = board[si][sj];
        boolean[][] visited = new boolean[rows][cols];
        Stack<int[]> stack = new Stack<>();
        int count = 0;
        String subString = String.valueOf(start);
        
        int[] startPosition = new int[2];
        startPosition[0] = si;
        startPosition[1] = sj;
        stack.push(startPosition);
        
        while(!stack.isEmpty()) {
            int size = stack.size();
            for(int layer = 0; layer < size; layer++) {
                int[] point = stack.pop();
                for(int[] dirc : dircs) {
                    if(isNext(words, point[0]+dirc[0], point[1]+dirc[1])) {
                        subString += String.valueOf(board[point[0]+dirc[0]][point[1]+dirc[1]])
                    }
                }
            }
            count++;
        }
    }
    private boolean isNext(String[] words, int row, int col) {
        //
    }
}

解答

jiuzhang solution: DFS
用一个hashmap来存储所有的前缀和目标单词,并且用boolean型的value来区分二者。DFS递归检测到有目标单词就加入到结果list中

class Solution {
    int[] dx = {0, 0, -1, 1};
    int[] dy = {-1, 1, 0, 0};
    
    public List<String> findWords(char[][] board, String[] words) {
        if (board == null || board.length == 0) {
            return new ArrayList<>();
        }
        if (board[0] == null || board[0].length == 0) {
            return new ArrayList<>();
        }
        HashMap<String, Boolean> isPrefix = getPrefix(words);
        HashSet<String> res = new HashSet<>();
        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++) {
                dfs(res, isPrefix, visited, board, i, j, String.valueOf(board[i][j]));
                visited[i][j] = false;
            }
        }
        return new ArrayList<String>(res);
    }
    
    private HashMap<String, Boolean> getPrefix(String[] words) {
        HashMap<String, Boolean> isPrefix = new HashMap<>();
        for(String word : words) {
            for(int i = 1; i < word.length(); i++) {
                if(isPrefix.containsKey(word.substring(0, i))) {
                    continue;
                }
                isPrefix.put(word.substring(0, i), true);
            }
            isPrefix.put(word, false);
        }
        return isPrefix;
    }
    
    private void dfs(HashSet<String> res, 
                     HashMap<String, Boolean> isPrefix, 
                     boolean[][] visited,
                     char[][] board,
                     int row, int col, String s) {
        if(!isPrefix.containsKey(s)) {
                return;
            }
        if(!isPrefix.get(s)) {
            res.add(s);
            //return;
        }
        visited[row][col] = true;
        for(int i = 0; i < 4; i++) {
            int newRow = row + dx[i];
            int newCol = col + dy[i];
            if(!isValid(newRow, newCol, board.length, board[0].length) ||
                visited[newRow][newCol]) {
                continue;
            }
            String temp = s + board[newRow][newCol];
            dfs(res, isPrefix, visited, board, newRow, newCol, temp);
            visited[newRow][newCol] = false;
        }
    }
    
    private boolean isValid(int row, int col, int rows, int cols) {
        if(row >= 0 && row < rows && col >= 0 && col < cols) {
            return true;
        }
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值