private int ex(char[][] a, String word, int n, int i, int j) {
        if (n == word.length()) return 0;
        int r = 1;
        char c = word.charAt(n);

        if (j < a[i].length - 1 && a[i][j + 1] == c && r != 0) {
            char temp = a[i][j + 1];
            a[i][j + 1] = '.';
            r *= ex(a, word, n + 1, i, j + 1);
            a[i][j + 1] = temp;
        }
        if (i < a.length - 1 && a[i + 1][j] == c && r != 0) {
            char temp = a[i + 1][j];
            a[i + 1][j] = '.';
            r *= ex(a, word, n + 1, i + 1, j);
            a[i + 1][j] = temp;
        }
        if (j > 0 && a[i][j - 1] == c && r != 0) {
            char temp = a[i][j - 1];
            a[i][j - 1] = '.';
            r *= ex(a, word, n + 1, i, j - 1);
            a[i][j - 1] = temp;
        }
        if (i > 0 && a[i - 1][j] == c && r != 0) {
            char temp = a[i - 1][j];
            a[i - 1][j] = '.';
            r *= ex(a, word, n + 1, i - 1, j);
            a[i - 1][j] = temp;
        }
        return r;
    }
This is recursive solution without using global variable
src/problems/medium/WordSearch.java