From 2cc15f8f01aceab7f6eeb87d19513077fad9d062 Mon Sep 17 00:00:00 2001 From: Sethu Manickam Date: Tue, 4 Nov 2025 20:57:49 -0800 Subject: [PATCH 1/2] Implement word search algorithm in problem2.java --- problem2.java | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 problem2.java diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..1237c1c0 --- /dev/null +++ b/problem2.java @@ -0,0 +1,41 @@ +class Solution { + int[][] DIRS = new int[][] {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; + int m, n; + boolean exists; + public boolean exist(char[][] board, String word) { + if (word == null || word.length() == 0) return true; + + m = board.length; + n = board[0].length; + exists = false; + + int[][] visited = new int[m][n]; + for (int i=0; i=0 && newJ>=0 && newI Date: Tue, 4 Nov 2025 20:58:04 -0800 Subject: [PATCH 2/2] Implement N-Queens solution using backtracking --- problem1.java | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 problem1.java diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..ae134a2e --- /dev/null +++ b/problem1.java @@ -0,0 +1,50 @@ +class Solution { + List> result; + int n; + public List> solveNQueens(int n) { + char[][] board = new char[n][n]; + for (int i=0; i(); + this.n = n; + + backtrack(board, new HashSet<>(), new HashSet<>(), new HashSet<>(), 0); + + return result; + } + + private void backtrack(char[][] board, Set colSet, Set negativeDiagSet, Set positiveDiagSet, int row) { + + if (row == n) { + List list = new ArrayList<>(); + for (int k=0; k