Skip to content

Commit 96d94d6

Browse files
authored
Added tasks 93, 95, 97.
1 parent 1a10039 commit 96d94d6

File tree

10 files changed

+312
-0
lines changed

10 files changed

+312
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,8 +1531,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15311531
| 0102 |[Binary Tree Level Order Traversal](src/main/kotlin/g0101_0200/s0102_binary_tree_level_order_traversal/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_1_Day_6_Tree, Udemy_Tree_Stack_Queue | 355 | 29.37
15321532
| 0101 |[Symmetric Tree](src/main/kotlin/g0101_0200/s0101_symmetric_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_11_Tree, Level_2_Day_15_Tree | 290 | 26.98
15331533
| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue | 330 | 41.38
1534+
| 0097 |[Interleaving String](src/main/kotlin/g0001_0100/s0097_interleaving_string/Solution.kt)| Medium | String, Dynamic_Programming | 240 | 57.50
15341535
| 0096 |[Unique Binary Search Trees](src/main/kotlin/g0001_0100/s0096_unique_binary_search_trees/Solution.kt)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11 | 237 | 26.76
1536+
| 0095 |[Unique Binary Search Trees II](src/main/kotlin/g0001_0100/s0095_unique_binary_search_trees_ii/Solution.kt)| Medium | Dynamic_Programming, Tree, Binary_Tree, Backtracking, Binary_Search_Tree | 360 | 41.38
15351537
| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue | 283 | 17.97
1538+
| 0093 |[Restore IP Addresses](src/main/kotlin/g0001_0100/s0093_restore_ip_addresses/Solution.kt)| Medium | String, Backtracking | 304 | 73.33
15361539
| 0092 |[Reverse Linked List II](src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii/Solution.kt)| Medium | Linked_List | 191 | 82.35
15371540
| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways/Solution.kt)| Medium | Top_Interview_Questions, String, Dynamic_Programming, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_10 | 327 | 17.68
15381541
| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii/Solution.kt)| Medium | Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking | 366 | 58.09
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0001_0100.s0093_restore_ip_addresses
2+
3+
// #Medium #String #Backtracking #2022_09_26_Time_304_ms_(73.33%)_Space_35.8_MB_(93.33%)
4+
5+
class Solution() {
6+
fun restoreIpAddresses(s: String): List<String> {
7+
val results: MutableList<String> = ArrayList()
8+
step(s, 0, IntArray(4), 0, results)
9+
return results
10+
}
11+
12+
fun step(s: String, pos: Int, octets: IntArray, count: Int, results: MutableList<String>) {
13+
if (count == 4 && pos == s.length) {
14+
results.add(
15+
octets[0].toString() + '.' +
16+
octets[1] +
17+
'.' +
18+
octets[2] +
19+
'.' +
20+
octets[3]
21+
)
22+
} else if (count < 4 && pos < 12) {
23+
var octet = 0
24+
for (i in 0..2) {
25+
if (pos + i < s.length) {
26+
val digit = s[pos + i] - '0'
27+
octet = octet * 10 + digit
28+
if (octet < 256) {
29+
octets[count] = octet
30+
step(s, pos + i + 1, octets, count + 1, results)
31+
}
32+
if (i == 0 && digit == 0) {
33+
break
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
93\. Restore IP Addresses
2+
3+
Medium
4+
5+
A **valid IP address** consists of exactly four integers separated by single dots. Each integer is between `0` and `255` (**inclusive**) and cannot have leading zeros.
6+
7+
* For example, `"0.1.2.201"` and `"192.168.1.1"` are **valid** IP addresses, but `"0.011.255.245"`, `"192.168.1.312"` and `"192.168@1.1"` are **invalid** IP addresses.
8+
9+
Given a string `s` containing only digits, return _all possible valid IP addresses that can be formed by inserting dots into_ `s`. You are **not** allowed to reorder or remove any digits in `s`. You may return the valid IP addresses in **any** order.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "25525511135"
14+
15+
**Output:** ["255.255.11.135","255.255.111.35"]
16+
17+
**Example 2:**
18+
19+
**Input:** s = "0000"
20+
21+
**Output:** ["0.0.0.0"]
22+
23+
**Example 3:**
24+
25+
**Input:** s = "101023"
26+
27+
**Output:** ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
28+
29+
**Constraints:**
30+
31+
* `1 <= s.length <= 20`
32+
* `s` consists of digits only.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g0001_0100.s0095_unique_binary_search_trees_ii
2+
3+
// #Medium #Dynamic_Programming #Tree #Binary_Tree #Backtracking #Binary_Search_Tree
4+
// #2022_09_26_Time_360_ms_(41.38%)_Space_42.4_MB_(72.41%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/**
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
fun generateTrees(n: Int): List<TreeNode?> {
20+
var result: MutableList<TreeNode?> = ArrayList<TreeNode?>()
21+
result.add(TreeNode(1))
22+
for (i in 2..n) {
23+
val nresult: MutableList<TreeNode?> = ArrayList<TreeNode?>()
24+
for (r in result) {
25+
var node = TreeNode(i, r, null)
26+
nresult.add(node)
27+
var previous: TreeNode? = r
28+
while (previous != null) {
29+
node = TreeNode(i)
30+
val cr: TreeNode? = copy(r)
31+
insert(cr, node, previous)
32+
previous = node.left
33+
nresult.add(cr)
34+
}
35+
}
36+
result = nresult
37+
}
38+
return result
39+
}
40+
41+
private fun insert(dest: TreeNode?, n: TreeNode, from: TreeNode) {
42+
if (dest != null && dest.`val` === from.`val`) {
43+
val h: TreeNode? = dest.right
44+
dest.right = n
45+
n.left = h
46+
return
47+
}
48+
if (dest != null) {
49+
insert(dest.right, n, from)
50+
}
51+
}
52+
53+
private fun copy(n: TreeNode?): TreeNode? {
54+
return if (n == null) {
55+
null
56+
} else TreeNode(n.`val`, copy(n.left), copy(n.right))
57+
}
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
95\. Unique Binary Search Trees II
2+
3+
Medium
4+
5+
Given an integer `n`, return _all the structurally unique **BST'**s (binary search trees), which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`. Return the answer in **any order**.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg)
10+
11+
**Input:** n = 3
12+
13+
**Output:** [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
14+
15+
**Example 2:**
16+
17+
**Input:** n = 1
18+
19+
**Output:** [[1]]
20+
21+
**Constraints:**
22+
23+
* `1 <= n <= 8`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0001_0100.s0097_interleaving_string
2+
3+
// #Medium #String #Dynamic_Programming #2022_09_26_Time_240_ms_(57.50%)_Space_35.1_MB_(57.50%)
4+
5+
class Solution {
6+
fun isInterleave(s1: String, s2: String, s3: String): Boolean {
7+
if (s3.length != s1.length + s2.length) {
8+
return false
9+
}
10+
val cache = Array(s1.length + 1) { arrayOfNulls<Boolean>(s2.length + 1) }
11+
return isInterleave(s1, s2, s3, 0, 0, 0, cache)
12+
}
13+
14+
fun isInterleave(
15+
s1: String,
16+
s2: String,
17+
s3: String,
18+
i1: Int,
19+
i2: Int,
20+
i3: Int,
21+
cache: Array<Array<Boolean?>>
22+
): Boolean {
23+
if (cache[i1][i2] != null) {
24+
return cache[i1][i2]!!
25+
}
26+
if (i1 == s1.length && i2 == s2.length && i3 == s3.length) {
27+
return true
28+
}
29+
var result = false
30+
if (i1 < s1.length && s1[i1] == s3[i3]) {
31+
result = isInterleave(s1, s2, s3, i1 + 1, i2, i3 + 1, cache)
32+
}
33+
if (i2 < s2.length && s2[i2] == s3[i3]) {
34+
result = result || isInterleave(s1, s2, s3, i1, i2 + 1, i3 + 1, cache)
35+
}
36+
cache[i1][i2] = result
37+
return result
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
97\. Interleaving String
2+
3+
Medium
4+
5+
Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
6+
7+
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` **non-empty** substrings respectively, such that:
8+
9+
* <code>s = s<sub>1</sub> + s<sub>2</sub> + ... + s<sub>n</sub></code>
10+
* <code>t = t<sub>1</sub> + t<sub>2</sub> + ... + t<sub>m</sub></code>
11+
* `|n - m| <= 1`
12+
* The **interleaving** is <code>s<sub>1</sub> + t<sub>1</sub> + s<sub>2</sub> + t<sub>2</sub> + s<sub>3</sub> + t<sub>3</sub> + ...</code> or <code>t<sub>1</sub> + s<sub>1</sub> + t<sub>2</sub> + s<sub>2</sub> + t<sub>3</sub> + s<sub>3</sub> + ...</code>
13+
14+
**Note:** `a + b` is the concatenation of strings `a` and `b`.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg)
19+
20+
**Input:** s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
21+
22+
**Output:** true
23+
24+
**Explanation:** One way to obtain s3 is: Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a". Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac". Since s3 can be obtained by interleaving s1 and s2, we return true.
25+
26+
**Example 2:**
27+
28+
**Input:** s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
29+
30+
**Output:** false
31+
32+
**Explanation:** Notice how it is impossible to interleave s2 with any other string to obtain s3.
33+
34+
**Example 3:**
35+
36+
**Input:** s1 = "", s2 = "", s3 = ""
37+
38+
**Output:** true
39+
40+
**Constraints:**
41+
42+
* `0 <= s1.length, s2.length <= 100`
43+
* `0 <= s3.length <= 200`
44+
* `s1`, `s2`, and `s3` consist of lowercase English letters.
45+
46+
**Follow up:** Could you solve it using only `O(s2.length)` additional memory space?
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0001_0100.s0093_restore_ip_addresses
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun restoreIpAddresses() {
10+
assertThat(
11+
Solution().restoreIpAddresses("25525511135").toString(),
12+
equalTo("[255.255.11.135, 255.255.111.35]")
13+
)
14+
}
15+
16+
@Test
17+
fun restoreIpAddresses2() {
18+
assertThat(Solution().restoreIpAddresses("0000").toString(), equalTo("[0.0.0.0]"))
19+
}
20+
21+
@Test
22+
fun restoreIpAddresses3() {
23+
assertThat(
24+
Solution().restoreIpAddresses("101023").toString(),
25+
equalTo("[1.0.10.23, 1.0.102.3, 10.1.0.23, 10.10.2.3, 101.0.2.3]")
26+
)
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0095_unique_binary_search_trees_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun generateTrees() {
10+
assertThat(
11+
Solution().generateTrees(3).toString(),
12+
equalTo(
13+
"[3,2,1,null,null, 2,1,3, 3,1,null,2,null, 1,null,3,2,null, 1,null,2,null,3]"
14+
)
15+
)
16+
}
17+
18+
@Test
19+
fun generateTrees2() {
20+
assertThat(Solution().generateTrees(1).toString(), equalTo("[1]"))
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0097_interleaving_string
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun isInterleave() {
10+
assertThat(Solution().isInterleave("aabcc", "dbbca", "aadbbcbcac"), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isInterleave2() {
15+
assertThat(Solution().isInterleave("aabcc", "dbbca", "aadbbbaccc"), equalTo(false))
16+
}
17+
18+
@Test
19+
fun isInterleave3() {
20+
assertThat(Solution().isInterleave("", "", ""), equalTo(true))
21+
}
22+
}

0 commit comments

Comments
 (0)