From b3e34f17b7dc04bd31b0434e4b1e0d341946c860 Mon Sep 17 00:00:00 2001 From: Marsh Royden Date: Tue, 27 Dec 2022 09:02:56 +0530 Subject: [PATCH 1/6] add solution for 51.N-Queens in Kotlin --- kotlin/51-N-Queens.kt | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 kotlin/51-N-Queens.kt diff --git a/kotlin/51-N-Queens.kt b/kotlin/51-N-Queens.kt new file mode 100644 index 000000000..6f41711cc --- /dev/null +++ b/kotlin/51-N-Queens.kt @@ -0,0 +1,55 @@ +package kotlin + +class Solution { + fun solveNQueens(n: Int): List> { + if (n == 1) return listOf(listOf("Q")) + val resultantList = mutableListOf>() + val board = MutableList(n) { + StringBuilder(".".repeat(n)) + } + val unavailableColumnPositions = hashSetOf() + val unavailableLeftDiagonalPositions = hashSetOf() + val unavailableRightDiagonalPositions = hashSetOf() + + // Queens indexed from 0. ie : Q_0,Q_1,Q_2....Q_n + @Suppress("UnnecessaryVariable") + fun dfs(nthQueen: Int = 0): Boolean { + if (nthQueen == n) { + val list = mutableListOf() + board.forEach { list.add(it.toString()) } + resultantList.add(list) + return false + } + // nthQueen needs to be inserted in the nth row. Therefore, + // the parameter "nthQueen" can also be considered as the row + // index. + val row = nthQueen + for (column in 0 until n) { + if ( + unavailableColumnPositions.contains(column) || + unavailableLeftDiagonalPositions.contains(row - column) || + unavailableRightDiagonalPositions.contains(row + column) + ) continue + + // place queen + board[row][column] = 'Q' + unavailableColumnPositions.add(column) + unavailableLeftDiagonalPositions.add(row - column) + unavailableRightDiagonalPositions.add(row + column) + + // try placing the next queen + val isQueenPlaced = dfs(nthQueen + 1) + if (isQueenPlaced) return true + + // remove queen + board[row][column] = '.' + unavailableColumnPositions.remove(column) + unavailableLeftDiagonalPositions.remove(nthQueen - column) + unavailableRightDiagonalPositions.remove(nthQueen + column) + } + return false + } + dfs() + return resultantList + } +} \ No newline at end of file From 1fba09eae729c67cec2c54318dc6a07e86351862 Mon Sep 17 00:00:00 2001 From: Marsh Royden Date: Tue, 27 Dec 2022 09:04:00 +0530 Subject: [PATCH 2/6] add solution for 17. Letter Combinations of a Phone Number in Kotlin --- ...7-Letter-Combinations-of-a-Phone-Number.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 kotlin/17-Letter-Combinations-of-a-Phone-Number.kt diff --git a/kotlin/17-Letter-Combinations-of-a-Phone-Number.kt b/kotlin/17-Letter-Combinations-of-a-Phone-Number.kt new file mode 100644 index 000000000..e8b0acb19 --- /dev/null +++ b/kotlin/17-Letter-Combinations-of-a-Phone-Number.kt @@ -0,0 +1,34 @@ +package kotlin + +class Solution { + fun letterCombinations(digits: String): List { + if (digits.isEmpty()) return emptyList() + val resultantList = mutableListOf() + val stringBuilder = StringBuilder() + val digitCharListMapping = mutableMapOf( + '2' to listOf('a', 'b', 'c'), + '3' to listOf('d', 'e', 'f'), + '4' to listOf('g', 'h', 'i'), + '5' to listOf('j', 'k', 'l'), + '6' to listOf('m', 'n', 'o'), + '7' to listOf('p', 'q', 'r', 's'), + '8' to listOf('t', 'u', 'v'), + '9' to listOf('w', 'x', 'y', 'z') + ) + + fun dfs(decisionIndex: Int = 0) { + if (stringBuilder.length == digits.length) { + resultantList.add(stringBuilder.toString()) + return + } + val charListForDigitAtDecisionIndex = digitCharListMapping.getValue(digits[decisionIndex]) + for (char in charListForDigitAtDecisionIndex) { + stringBuilder.append(char) + dfs(decisionIndex + 1) + stringBuilder.deleteCharAt(stringBuilder.lastIndex) + } + } + dfs() + return resultantList + } +} \ No newline at end of file From 2343fa7413037c3a7069c68513056d6f1ff992dd Mon Sep 17 00:00:00 2001 From: Bot-A0 <71089234+Ahmad-A0@users.noreply.github.com> Date: Tue, 27 Dec 2022 04:23:51 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=93=9C=20Update=20README=20table=20(?= =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20from=20Github=20Actions)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3271509ca..73748d7fe 100644 --- a/README.md +++ b/README.md @@ -262,13 +262,13 @@ If you would like to have collaborator permissions on the repo to merge your own [79 - Word Search](https://leetcode.com/problems/word-search/) |
[✔️](c%2F79-Word-Search.c)
|
[✔️](cpp%2F79-Word-Search.cpp)
|
[✔️](csharp%2F79-Word-Search.cs)
|
[✔️](go%2F79-Word-Search.go)
|
[✔️](java%2F79-Word-Search.java)
|
[✔️](javascript%2F79-Word-Search.js)
|
[✔️](kotlin%2F79-Word-Search.kt)
|
[✔️](python%2F79-Word-Search.py)
|
|
|
|
[✔️](swift%2F79-Word-Search.swift)
|
[131 - Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) |
|
[✔️](cpp%2F131-Palindrome-Partitioning.cpp)
|
[✔️](csharp%2F131-Palindrome-Partitioning.cs)
|
[✔️](go%2F131-Palindrome-Partitioning.go)
|
[✔️](java%2F131-Palindrome-Partitioning.java)
|
[✔️](javascript%2F131-Palindrome-Partitioning.js)
|
|
[✔️](python%2F131-Palindrome-Partitioning.py)
|
|
|
|
|
[✔️](typescript%2F131-Palindrome-Partitioning.ts)
[93 - Restore Ip Addresses](https://leetcode.com/problems/restore-ip-addresses/) |
|
|
|
|
|
[✔️](javascript%2F937-K-Closest-Points-To-Origin.js)
|
|
[✔️](python%2F931-Minimum-Falling-Path-Sum.py)
|
|
|
|
|
-[17 - Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) |
|
[✔️](cpp%2F17-Letter-Combinations-Phone-Number.cpp)
|
[✔️](csharp%2F17-Letter-Combinations-Of-A-Phone-Number.cs)
|
[✔️](go%2F17-Letter-Combinations-of-a-Phone-Number.go)
|
[✔️](java%2F17-Letter-Combinations-of-a-Phone-Number.java)
|
[✔️](javascript%2F17-Letter-Combinations-of-a-Phone-Number.js)
|
|
[✔️](python%2F17-Letter-Combinations-of-a-Phone-Number.py)
|
[✔️](ruby%2F17-Letter-Combinations-Of-A-Phone-Number.rb)
|
|
|
|
[✔️](typescript%2F17-Letter-Combinations-of-a-Phone-Number.ts)
+[17 - Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) |
|
[✔️](cpp%2F17-Letter-Combinations-Phone-Number.cpp)
|
[✔️](csharp%2F17-Letter-Combinations-Of-A-Phone-Number.cs)
|
[✔️](go%2F17-Letter-Combinations-of-a-Phone-Number.go)
|
[✔️](java%2F17-Letter-Combinations-of-a-Phone-Number.java)
|
[✔️](javascript%2F17-Letter-Combinations-of-a-Phone-Number.js)
|
[✔️](kotlin%2F17-Letter-Combinations-of-a-Phone-Number.kt)
|
[✔️](python%2F17-Letter-Combinations-of-a-Phone-Number.py)
|
[✔️](ruby%2F17-Letter-Combinations-Of-A-Phone-Number.rb)
|
|
|
|
[✔️](typescript%2F17-Letter-Combinations-of-a-Phone-Number.ts)
[473 - Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/) |
|
[✔️](cpp%2F473-Matchsticks-to-Square.cpp)
|
|
|
|
[✔️](javascript%2F473-Matchsticks-To-Square.js)
|
|
[✔️](python%2F473-Matchsticks-to-Square.py)
|
|
|
|
|
[1849 - Splitting a String Into Descending Consecutive Values](https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/) |
|
|
|
|
|
|
|
[✔️](python%2F1849-Splitting-A-String-Into-Descending-Consecutive-Values.py)
|
|
|
|
|
[1980 - Find Unique Binary String](https://leetcode.com/problems/find-unique-binary-string/) |
|
|
|
|
|
|
|
[✔️](python%2F1980-Find-Unique-Binary-String.py)
|
|
|
|
|
[1239 - Maximum Length of a Concatenated String With Unique Characters](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) |
|
|
|
|
|
|
|
[✔️](python%2F1239-Maximum-Length-of-a-Concatenated-String-with-Unique-Characters.py)
|
|
|
|
|
[698 - Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/) |
|
|
|
|
|
|
|
|
|
|
|
|
-[51 - N Queens](https://leetcode.com/problems/n-queens/) |
[✔️](c%2F518-Coin-Change-II.c)
|
[✔️](cpp%2F51-N-Queens.cpp)
|
[✔️](csharp%2F51-N-Queens.cs)
|
[✔️](go%2F51-N-Queens.go)
|
[✔️](java%2F51-N-Queens.java)
|
[✔️](javascript%2F51-solveNQueens.js)
|
|
[✔️](python%2F51-N-Queens.py)
|
|
|
|
|
[✔️](typescript%2F518-Coin-Change-II.ts)
+[51 - N Queens](https://leetcode.com/problems/n-queens/) |
[✔️](c%2F518-Coin-Change-II.c)
|
[✔️](cpp%2F51-N-Queens.cpp)
|
[✔️](csharp%2F51-N-Queens.cs)
|
[✔️](go%2F51-N-Queens.go)
|
[✔️](java%2F51-N-Queens.java)
|
[✔️](javascript%2F51-solveNQueens.js)
|
[✔️](kotlin%2F51-N-Queens.kt)
|
[✔️](python%2F51-N-Queens.py)
|
|
|
|
|
[✔️](typescript%2F518-Coin-Change-II.ts)
[52 - N Queens II](https://leetcode.com/problems/n-queens-ii/) |
[✔️](c%2F52-N-Queens-II.c)
|
[✔️](cpp%2F52-N-Queens-2.cpp)
|
|
|
[✔️](java%2F523-Continuous-Subarray-Sum.java)
|
[✔️](javascript%2F52-totalNQueens.js)
|
|
[✔️](python%2F523-Continuous-Subarray-Sum.py)
|
|
|
|
|
### Graphs @@ -318,7 +318,7 @@ If you would like to have collaborator permissions on the repo to merge your own [746 - Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) |
[✔️](c%2F746-Min-Cost-Climbing-Stairs.c)
|
[✔️](cpp%2F746-Min-Cost-Climbing-Stairs.cpp)
|
[✔️](csharp%2F746-Min-Cost-Climbing-Stairs.cs)
|
[✔️](go%2F746-Min-Cost-Climbing-Stairs.go)
|
[✔️](java%2F746-Min-Cost-Climbing-Stairs.java)
|
[✔️](javascript%2F746-Min-Cost-Climbing-Stairs.js)
|
[✔️](kotlin%2F746-Min-Cost-Climbing-Stairs.kt)
|
[✔️](python%2F746-Min-Cost-Climbing-Stairs.py)
|
[✔️](ruby%2F746-Min-Cost-Climbing-Stairs.rb)
|
[✔️](rust%2F746-Min-Cost-Climbing-Stairs.rs)
|
|
[✔️](swift%2F746-Min-Cost-Climbing-Stairs.swift)
|
[✔️](typescript%2F746-Min-Cost-Climbing-Stairs.ts)
[198 - House Robber](https://leetcode.com/problems/house-robber/) |
[✔️](c%2F198-House-Robber.c)
|
[✔️](cpp%2F198-House-Robber.cpp)
|
[✔️](csharp%2F198-House-Robber.cs)
|
[✔️](go%2F198-House-Robber.go)
|
[✔️](java%2F198-House-Robber.java)
|
[✔️](javascript%2F198-House-Robber.js)
|
[✔️](kotlin%2F198-House-Robber.kt)
|
[✔️](python%2F198-House-Robber.py)
|
[✔️](ruby%2F198-House-Robber.rb)
|
[✔️](rust%2F198-House-Robber.rs)
|
[✔️](scala%2F198-House-Robber.scala)
|
[✔️](swift%2F198-House-Robber.swift)
|
[✔️](typescript%2F198-House-Robber.ts)
[213 - House Robber II](https://leetcode.com/problems/house-robber-ii/) |
[✔️](c%2F213-House-Robber-II.c)
|
[✔️](cpp%2F213-House-Robber-II.cpp)
|
[✔️](csharp%2F213-House-Robber-II.cs)
|
[✔️](go%2F213-House-Robber-II.go)
|
[✔️](java%2F213-House-Robber-II.java)
|
[✔️](javascript%2F213-House-Robber-II.js)
|
[✔️](kotlin%2F213-House-Robber-II.kt)
|
[✔️](python%2F213-House-Robber-II.py)
|
[✔️](ruby%2F213-House-Robber-II.rb)
|
[✔️](rust%2F213-House-Robber-II.rs)
|
[✔️](scala%2F213-House-Robber-II.scala)
|
[✔️](swift%2F213-House-Robber-II.swift)
|
[✔️](typescript%2F213-House-Robber-II.ts)
-[5 - Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) |
[✔️](c%2F5-Longest-Palindromic-Substring.c)
|
[✔️](cpp%2F5-Longest-Palindrome-Substring.cpp)
|
[✔️](csharp%2F5-Longest-Palindromic-Substring.cs)
|
[✔️](go%2F51-N-Queens.go)
|
[✔️](java%2F5-Longest-Palindromic-Substring.java)
|
[✔️](javascript%2F5-Longest-Palindromic-Substring.js)
|
[✔️](kotlin%2F53-Maximum-Subarray.kt)
|
[✔️](python%2F5-Longest-Palindromic-Substring.py)
|
[✔️](ruby%2F543-Diameter-of-Binary-Tree.rb)
|
[✔️](rust%2F5-Longest-Palindromic-Substring.rs)
|
[✔️](scala%2F56-Merge-Intervals.scala)
|
[✔️](swift%2F53-Maximum-Subarray.swift)
|
[✔️](typescript%2F5-Longest-Palindromic-Substring.ts)
+[5 - Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) |
[✔️](c%2F5-Longest-Palindromic-Substring.c)
|
[✔️](cpp%2F5-Longest-Palindrome-Substring.cpp)
|
[✔️](csharp%2F5-Longest-Palindromic-Substring.cs)
|
[✔️](go%2F51-N-Queens.go)
|
[✔️](java%2F5-Longest-Palindromic-Substring.java)
|
[✔️](javascript%2F5-Longest-Palindromic-Substring.js)
|
[✔️](kotlin%2F51-N-Queens.kt)
|
[✔️](python%2F5-Longest-Palindromic-Substring.py)
|
[✔️](ruby%2F543-Diameter-of-Binary-Tree.rb)
|
[✔️](rust%2F5-Longest-Palindromic-Substring.rs)
|
[✔️](scala%2F56-Merge-Intervals.scala)
|
[✔️](swift%2F53-Maximum-Subarray.swift)
|
[✔️](typescript%2F5-Longest-Palindromic-Substring.ts)
[647 - Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) |
[✔️](c%2F647-Palindromic-Substrings.c)
|
[✔️](cpp%2F647-Palindromic-Substrings.cpp)
|
[✔️](csharp%2F647-Palindromic-Substrings.cs)
|
|
[✔️](java%2F647-Palindromic-Substrings.java)
|
[✔️](javascript%2F647-Palindromic-Substrings.js)
|
|
[✔️](python%2F647-Palindromic-Substrings.py)
|
|
[✔️](rust%2F647-Palindromic-Substrings.rs)
|
|
|
[✔️](typescript%2F647-Palindromic-Substrings.ts)
[91 - Decode Ways](https://leetcode.com/problems/decode-ways/) |
[✔️](c%2F91-Decode-Ways.c)
|
[✔️](cpp%2F91-Decode-Ways.cpp)
|
[✔️](csharp%2F91-Decode-Ways.cs)
|
|
[✔️](java%2F91-Decode-Ways.java)
|
[✔️](javascript%2F91-Decode-Ways.js)
|
[✔️](kotlin%2F91-Decode-Ways.kt)
|
[✔️](python%2F91-Decode-ways.py)
|
|
|
[✔️](scala%2F91-Decode-Ways.scala)
|
|
[✔️](typescript%2F91-Decode-Ways.ts)
[322 - Coin Change](https://leetcode.com/problems/coin-change/) |
[✔️](c%2F322-Coin-Change.c)
|
[✔️](cpp%2F322-Coin-Change.cpp)
|
[✔️](csharp%2F322-Coin-Change.cs)
|
|
[✔️](java%2F322-Coin-Change.java)
|
[✔️](javascript%2F322-Coin-Change.js)
|
[✔️](kotlin%2F322-Coin-Change.kt)
|
[✔️](python%2F322-Coin-Change.py)
|
|
[✔️](rust%2F322-Coin-Change.rs)
|
|
|
[✔️](typescript%2F322-Coin-Change.ts)
From c8b75a7a1fc3584b674ebf5cd3f8716146e678c2 Mon Sep 17 00:00:00 2001 From: Technophile <54663474+t3chkid@users.noreply.github.com> Date: Sat, 25 Feb 2023 07:37:20 +0530 Subject: [PATCH 4/6] Delete 51-N-Queens.kt --- kotlin/51-N-Queens.kt | 55 ------------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 kotlin/51-N-Queens.kt diff --git a/kotlin/51-N-Queens.kt b/kotlin/51-N-Queens.kt deleted file mode 100644 index 6f41711cc..000000000 --- a/kotlin/51-N-Queens.kt +++ /dev/null @@ -1,55 +0,0 @@ -package kotlin - -class Solution { - fun solveNQueens(n: Int): List> { - if (n == 1) return listOf(listOf("Q")) - val resultantList = mutableListOf>() - val board = MutableList(n) { - StringBuilder(".".repeat(n)) - } - val unavailableColumnPositions = hashSetOf() - val unavailableLeftDiagonalPositions = hashSetOf() - val unavailableRightDiagonalPositions = hashSetOf() - - // Queens indexed from 0. ie : Q_0,Q_1,Q_2....Q_n - @Suppress("UnnecessaryVariable") - fun dfs(nthQueen: Int = 0): Boolean { - if (nthQueen == n) { - val list = mutableListOf() - board.forEach { list.add(it.toString()) } - resultantList.add(list) - return false - } - // nthQueen needs to be inserted in the nth row. Therefore, - // the parameter "nthQueen" can also be considered as the row - // index. - val row = nthQueen - for (column in 0 until n) { - if ( - unavailableColumnPositions.contains(column) || - unavailableLeftDiagonalPositions.contains(row - column) || - unavailableRightDiagonalPositions.contains(row + column) - ) continue - - // place queen - board[row][column] = 'Q' - unavailableColumnPositions.add(column) - unavailableLeftDiagonalPositions.add(row - column) - unavailableRightDiagonalPositions.add(row + column) - - // try placing the next queen - val isQueenPlaced = dfs(nthQueen + 1) - if (isQueenPlaced) return true - - // remove queen - board[row][column] = '.' - unavailableColumnPositions.remove(column) - unavailableLeftDiagonalPositions.remove(nthQueen - column) - unavailableRightDiagonalPositions.remove(nthQueen + column) - } - return false - } - dfs() - return resultantList - } -} \ No newline at end of file From 4a007211242d4ba5e0e3a02ebd7a5b1fa151f51a Mon Sep 17 00:00:00 2001 From: Bot-A0 <71089234+Ahmad-A0@users.noreply.github.com> Date: Sat, 25 Feb 2023 02:51:01 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=93=9C=20Update=20README=20table=20(?= =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20from=20Github=20Actions)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 491132edf..9165b49c3 100644 --- a/README.md +++ b/README.md @@ -437,4 +437,4 @@ If you would like to have collaborator permissions on the repo to merge your own --- -Need to update the README? [Update the template instead.](README_template.md) \ No newline at end of file +Need to update the README? [Update the template instead.](README_template.md) From ae28643606bc40ad9e8e2a1f0a76ba02d4893956 Mon Sep 17 00:00:00 2001 From: Ali A Date: Sat, 25 Feb 2023 12:43:32 +0100 Subject: [PATCH 6/6] Rename 17-Letter-Combinations-of-a-Phone-Number.kt to 0017-letter-combinations-of-a-phone-number.kt --- ...-Number.kt => 0017-letter-combinations-of-a-phone-number.kt} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename kotlin/{17-Letter-Combinations-of-a-Phone-Number.kt => 0017-letter-combinations-of-a-phone-number.kt} (99%) diff --git a/kotlin/17-Letter-Combinations-of-a-Phone-Number.kt b/kotlin/0017-letter-combinations-of-a-phone-number.kt similarity index 99% rename from kotlin/17-Letter-Combinations-of-a-Phone-Number.kt rename to kotlin/0017-letter-combinations-of-a-phone-number.kt index e8b0acb19..deb298273 100644 --- a/kotlin/17-Letter-Combinations-of-a-Phone-Number.kt +++ b/kotlin/0017-letter-combinations-of-a-phone-number.kt @@ -31,4 +31,4 @@ class Solution { dfs() return resultantList } -} \ No newline at end of file +}