leetcode #79 in cpp

原创 2016年06月02日 09:50:56

Given a 2D board and a word, find if the word exists in the grid.

The word can 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.

For example,
Given board =

[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

Solution;

This is a basic DFS search for string. Simply find the first character and perform DFS in 4 direction for next characters. 

Code:

class Solution {
public:
    int dir[4][2] = {{1,0},{0,1}, {-1, 0}, {0,-1}}; 
    
    bool exist(vector<vector<char>>& board, string word) {
        if(board.empty()) return false;
        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 ++){
                if(board[i][j] == word[0]){//find the match of the first character, then start from this point to locate the string
                    visited[i][j] = true;//note this position as visited. This is because 1 letter could be used once. 
                    if(search(i, j, 1,visited, word, board))
                        return true;
                    else visited[i][j] = false;//if next characters not found, go to find another starting point
                }
            }
        }
        return false; 
    }
    
    bool search(int cur_row, int cur_col,int search_pos, vector<vector<bool>> &visited, string &word, vector<vector<char>> &board)
    {   
        if(search_pos >= word.length()) return true;//if searching position is over the string length, 
<span style="white-space:pre">						</span>//it means we successfully reach the end
        //search for the letter at search_ind
        for(int i = 0; i < 4; i ++){//search for 4 directions
            int row = cur_row + dir[i][0];
            int col = cur_col + dir[i][1];
<span style="white-space:pre">		</span>//see if this position is out of bound and if this position has the character we want to find. If character matches,
<span style="white-space:pre">		</span>// go to the next recurrence for next character
            if(row < board.size() && row >=0 && col < board[0].size() && col >= 0 && !visited[row][col] && board[row][col] == word[search_pos]){
                visited[row][col] = true;
                if(search(row, col, search_pos+1, visited, word, board))
                    return true;
                else
                    visited[row][col] = false;
            }
        }
        return false;
    }
};


Leetcode 79. Word Search (Medium) (cpp)

Leetcode 79. Word Search (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年07月31日 22:26
  • 175

leetcode #86 in cpp

Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr...

leetcode #24 in cpp

The question is to swap each pair in the linked list. And the requirement is to use constant space. ...

Leetcode 450. Delete Node in a BST (Medium) (cpp)

Leetcode 450. Delete Node in a BST (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月26日 07:50
  • 285

leetcode #36 in cpp.

The question is to determine a Sudoku is valid.  Solution:  if a Sudoku is valid, each row, each co...

*leetcode 138 in cpp

A linked list is given such that each node contains an additional random pointer which could point t...

leetcode #17 in cpp

The question is : Given a digit string, return all possible letter combinations that the number coul...

leetcode $42 in cpp

Code: (This method is not the best one. There are other methods using two pointers) Key: a bar coul...

*leetcode #80 in cpp

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, G...

Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)

Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月27日 02:35
  • 224
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #79 in cpp
举报原因:
原因补充:

(最多只允许输入30个字)