diff --git a/NQueens.kt b/NQueens.kt new file mode 100644 index 00000000..2dc9abc4 --- /dev/null +++ b/NQueens.kt @@ -0,0 +1,72 @@ +// In this problem, we need to place n queens on a n x n board following some rules. We use backtracking to solve this problem. We try to place q queen row by row and check if that position is a valid poistion. the way to check if the position is valid or not is by checking the portion above the current location. +// so we check if there is any queen in the rows above, on the top left diagnoal and top right diagonal. +// If the position is valid we place the queen and move to the next row. If we reach the end of the board, we add the current board configuration to the result. +// Time complexity is O(N!) where N is the number of queens. Space complexity is O(N^2) for the board. +class Solution { + val result = mutableListOf>() + fun solveNQueens(n: Int): List> { + val board = Array(n) { BooleanArray(n) } + helper(board, 0, n) + return result + } + + fun helper(board: Array, row: Int, n: Int) { + //base + if (row == n) { + val list = mutableListOf() + for(i in 0 until n) { + var sb = StringBuilder() + for(j in 0 until n) { + if(board[i][j]) { + sb.append('Q') + } else { + sb.append('.') + } + } + list.add(sb.toString()) + } + result.add(list) + return + } + //action + + for(j in 0 until n) { + if(isValid(board, row, j, n)) { + board[row][j] = true + helper(board, row + 1, n) + board[row][j] = false + } + } + } + + fun isValid(board: Array, i: Int, j: Int, n: Int) : Boolean { + var row = i + var col = j + + //check straight up + while(row>= 0) { + if(board[row][col]) return false + row-- + } + + row = i + col = j + //check left up + while(row >= 0 && col >= 0) { + if(board[row][col]) return false + row-- + col-- + } + + row = i + col = j + //check right up + while(row >= 0 && col < n) { + if(board[row][col]) return false + row-- + col++ + } + + return true + } +} \ No newline at end of file diff --git a/WordSearch.kt b/WordSearch.kt new file mode 100644 index 00000000..c04d3473 --- /dev/null +++ b/WordSearch.kt @@ -0,0 +1,45 @@ +// In this problem, we need to find if a give word exists in the board by moving in all 4 directions. We use backtracking to solve this problem. We start from each cell and try to find +// the word by moving in all 4 directions. We mark the cell as visited by changing the character to a special character and backtrack after exploring all possibilities from that cell. +// Time complexity is O(M * N * 3^L) where M is number of rows, N is number of columns and L is length of the word. Space complexity is O(L) for the recursion stack. + +class Solution { + val dirs = arrayOf( + intArrayOf(-1, 0), + intArrayOf(1, 0), + intArrayOf(0, -1), + intArrayOf(0, 1) + ) + var m = 0 + var n = 0 + fun exist(board: Array, word: String): Boolean { + m = board.size + n = board[0].size + + for(i in 0 until m) { + for(j in 0 until n) { + if(dfs(board, word, i, j, 0)) return true + } + + } + return false + + } + + fun dfs(board: Array, word: String, i: Int, j : Int, idx: Int) : Boolean { + //base + if(idx == word.length) return true + if(i < 0 || j <0 || i == m || j == n || board[i][j] == '#') return false + if(board[i][j] != word[idx]) return false + //logic + board[i][j] = '#' + for(dir in dirs) { + val r = dir[0] + i + val c = dir[1] + j + + if(dfs(board, word, r, c, idx + 1)) return true + } + + board[i][j] = word[idx] + return false + } +} \ No newline at end of file