Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions NQueens.kt
Original file line number Diff line number Diff line change
@@ -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<List<String>>()
fun solveNQueens(n: Int): List<List<String>> {
val board = Array(n) { BooleanArray(n) }
helper(board, 0, n)
return result
}

fun helper(board: Array<BooleanArray>, row: Int, n: Int) {
//base
if (row == n) {
val list = mutableListOf<String>()
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<BooleanArray>, 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
}
}
45 changes: 45 additions & 0 deletions WordSearch.kt
Original file line number Diff line number Diff line change
@@ -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<CharArray>, 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<CharArray>, 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
}
}