Here is the problem of leetcode. Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of a sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
I simply did dfs with keeping the check on the length of the word given.
class Solution {
    public:
        int n,m;
        bool check(int i,int j){
            if(i<0||j<0||i>=n||j>=m)return false;
            return true; 
        }
        int ans;
    void dfs(vector<vector<char>>& board,string word,int i,int j,int k){       
        if(!check(i,j) || board[i][j]!=word[k] || board[i][j]=='*')return  ;
        char x=board[i][j];
        board[i][j]='*';
        if(k==word.length()-1){
            ans=1;
            return ;
        }
        //cout<<s<<endl;
        dfs(board,word,i+1,j,k+1);
        dfs(board,word,i-1,j,k+1);
        dfs(board,word,i,j+1,k+1);
        dfs(board,word,i,j-1,k+1);
        board[i][j]=x;
    }
    bool exist(vector<vector<char>>& board, string word) {
        n=board.size();m=board[0].size();
        ans=0;
        int k=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                dfs(board,word,i,j,k);
                if(ans==1)return true;
            }
        }
        return ans;
    }
};
TLE for the given input: https://leetcode.com/submissions/detail/243435697/testcase/
