Skip to content

Commit c138ef9

Browse files
authored
Added tasks 940, 941, 942, 943
1 parent dae8b90 commit c138ef9

File tree

13 files changed

+390
-0
lines changed

13 files changed

+390
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,6 +1733,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17331733
|------|----------------|-------------|-------------|----------|---------
17341734
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17351735
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
1736+
| 0943 |[Find the Shortest Superstring](src/main/kotlin/g0901_1000/s0943_find_the_shortest_superstring/Solution.kt)| Hard | Array, String, Dynamic_Programming, Bit_Manipulation, Bitmask | 1290 | 100.00
1737+
| 0942 |[DI String Match](src/main/kotlin/g0901_1000/s0942_di_string_match/Solution.kt)| Easy | Array, String, Math, Greedy, Two_Pointers | 202 | 80.00
1738+
| 0941 |[Valid Mountain Array](src/main/kotlin/g0901_1000/s0941_valid_mountain_array/Solution.kt)| Easy | Array | 251 | 51.94
1739+
| 0940 |[Distinct Subsequences II](src/main/kotlin/g0901_1000/s0940_distinct_subsequences_ii/Solution.kt)| Hard | String, Dynamic_Programming | 177 | 100.00
17361740
| 0939 |[Minimum Area Rectangle](src/main/kotlin/g0901_1000/s0939_minimum_area_rectangle/Solution.kt)| Medium | Array, Hash_Table, Math, Sorting, Geometry | 461 | 100.00
17371741
| 0938 |[Range Sum of BST](src/main/kotlin/g0901_1000/s0938_range_sum_of_bst/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Udemy_Tree_Stack_Queue | 356 | 55.36
17381742
| 0937 |[Reorder Data in Log Files](src/main/kotlin/g0901_1000/s0937_reorder_data_in_log_files/Solution.kt)| Easy | Array, String, Sorting | 205 | 81.82
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0901_1000.s0940_distinct_subsequences_ii
2+
3+
// #Hard #String #Dynamic_Programming #2023_04_29_Time_177_ms_(100.00%)_Space_35.9_MB_(100.00%)
4+
5+
class Solution {
6+
fun distinctSubseqII(s: String): Int {
7+
val n = s.length
8+
val mod = 1000000007
9+
val arr = IntArray(26)
10+
for (i in 0 until n) {
11+
val x = s[i].code - 'a'.code
12+
var sum: Long = 0
13+
arr[x] += 1
14+
for (j in 0..25) {
15+
sum = (sum + arr[j]) % mod
16+
}
17+
arr[x] = sum.toInt()
18+
}
19+
var total: Long = 0
20+
for (x in arr) {
21+
total = (total + x) % mod
22+
}
23+
return total.toInt()
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
940\. Distinct Subsequences II
2+
3+
Hard
4+
5+
Given a string s, return _the number of **distinct non-empty subsequences** of_ `s`. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
6+
7+
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `"ace"` is a subsequence of <code>"<ins>a</ins>b<ins>c</ins>d<ins>e</ins>"</code> while `"aec"` is not.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "abc"
12+
13+
**Output:** 7
14+
15+
**Explanation:** The 7 distinct subsequences are "a", "b", "c", "ab", "ac", "bc", and "abc".
16+
17+
**Example 2:**
18+
19+
**Input:** s = "aba"
20+
21+
**Output:** 6
22+
23+
**Explanation:** The 6 distinct subsequences are "a", "b", "ab", "aa", "ba", and "aba".
24+
25+
**Example 3:**
26+
27+
**Input:** s = "aaa"
28+
29+
**Output:** 3
30+
31+
**Explanation:** The 3 distinct subsequences are "a", "aa" and "aaa".
32+
33+
**Constraints:**
34+
35+
* `1 <= s.length <= 2000`
36+
* `s` consists of lowercase English letters.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0901_1000.s0941_valid_mountain_array
2+
3+
// #Easy #Array #2023_04_29_Time_251_ms_(51.94%)_Space_55.5_MB_(7.75%)
4+
5+
class Solution {
6+
fun validMountainArray(arr: IntArray): Boolean {
7+
var i = 0
8+
var flag1 = false
9+
var flag2 = false
10+
while (i < arr.size - 1 && arr[i] < arr[i + 1]) {
11+
flag1 = true
12+
i++
13+
}
14+
while (i < arr.size - 1 && arr[i] > arr[i + 1]) {
15+
flag2 = true
16+
i++
17+
}
18+
if (i < arr.size - 1) {
19+
return false
20+
}
21+
return !(!flag1 || !flag2)
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
941\. Valid Mountain Array
2+
3+
Easy
4+
5+
Given an array of integers `arr`, return _`true` if and only if it is a valid mountain array_.
6+
7+
Recall that arr is a mountain array if and only if:
8+
9+
* `arr.length >= 3`
10+
* There exists some `i` with `0 < i < arr.length - 1` such that:
11+
* `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
12+
* `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`
13+
14+
![](https://assets.leetcode.com/uploads/2019/10/20/hint_valid_mountain_array.png)
15+
16+
**Example 1:**
17+
18+
**Input:** arr = [2,1]
19+
20+
**Output:** false
21+
22+
**Example 2:**
23+
24+
**Input:** arr = [3,5,5]
25+
26+
**Output:** false
27+
28+
**Example 3:**
29+
30+
**Input:** arr = [0,3,2,1]
31+
32+
**Output:** true
33+
34+
**Constraints:**
35+
36+
* <code>1 <= arr.length <= 10<sup>4</sup></code>
37+
* <code>0 <= arr[i] <= 10<sup>4</sup></code>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0901_1000.s0942_di_string_match
2+
3+
// #Easy #Array #String #Math #Greedy #Two_Pointers
4+
// #2023_04_29_Time_202_ms_(80.00%)_Space_45.6_MB_(10.00%)
5+
6+
class Solution {
7+
fun diStringMatch(s: String): IntArray {
8+
val arr = IntArray(s.length + 1)
9+
var max = s.length
10+
for (i in s.indices) {
11+
if (s[i] == 'D') {
12+
arr[i] = max
13+
max--
14+
}
15+
}
16+
run {
17+
var i = s.length - 1
18+
while (i >= 0 && max > 0) {
19+
if (s[i] == 'I' && arr[i + 1] == 0) {
20+
arr[i + 1] = max
21+
max--
22+
}
23+
i--
24+
}
25+
}
26+
var i = 0
27+
while (i < arr.size && max > 0) {
28+
if (arr[i] == 0) {
29+
arr[i] = max
30+
max--
31+
}
32+
i++
33+
}
34+
return arr
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
942\. DI String Match
2+
3+
Easy
4+
5+
A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where:
6+
7+
* `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and
8+
* `s[i] == 'D'` if `perm[i] > perm[i + 1]`.
9+
10+
Given a string `s`, reconstruct the permutation `perm` and return it. If there are multiple valid permutations perm, return **any of them**.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "IDID"
15+
16+
**Output:** [0,4,1,3,2]
17+
18+
**Example 2:**
19+
20+
**Input:** s = "III"
21+
22+
**Output:** [0,1,2,3]
23+
24+
**Example 3:**
25+
26+
**Input:** s = "DDI"
27+
28+
**Output:** [3,2,0,1]
29+
30+
**Constraints:**
31+
32+
* <code>1 <= s.length <= 10<sup>5</sup></code>
33+
* `s[i]` is either `'I'` or `'D'`.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g0901_1000.s0943_find_the_shortest_superstring
2+
3+
// #Hard #Array #String #Dynamic_Programming #Bit_Manipulation #Bitmask
4+
// #2023_04_29_Time_1290_ms_(100.00%)_Space_309.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun shortestSuperstring(words: Array<String>): String? {
8+
val l = words.size
9+
var state = 0
10+
for (i in 0 until l) {
11+
state = state or (1 shl i)
12+
}
13+
val map: MutableMap<String?, String?> = HashMap()
14+
return solveTPS(words, state, "", map, l)
15+
}
16+
17+
private fun solveTPS(
18+
words: Array<String>,
19+
state: Int,
20+
startWord: String,
21+
map: MutableMap<String?, String?>,
22+
l: Int
23+
): String? {
24+
val key = "$startWord|$state"
25+
if (state == 0) {
26+
return startWord
27+
}
28+
if (map[key] != null) {
29+
return map[key]
30+
}
31+
var minLenWord: String? = ""
32+
for (i in 0 until l) {
33+
if (state shr i and 1 == 1) {
34+
val takenState = state and (1 shl i).inv()
35+
val result = solveTPS(words, takenState, words[i], map, l)
36+
val tmp = mergeAndGet(startWord, result)
37+
if (minLenWord!!.isEmpty() || minLenWord.length > tmp.length) {
38+
minLenWord = tmp
39+
}
40+
}
41+
}
42+
map[key] = minLenWord
43+
return minLenWord
44+
}
45+
46+
private fun mergeAndGet(word: String, result: String?): String {
47+
val l = word.length
48+
val t = result!!.length
49+
if (result.contains(word)) {
50+
return result
51+
}
52+
if (word.contains(result)) {
53+
return word
54+
}
55+
var found = l
56+
for (k in 0 until l) {
57+
var i = k
58+
var j = 0
59+
while (i < l && j < t) {
60+
if (word[i] == result[j]) {
61+
i++
62+
j++
63+
} else break
64+
}
65+
if (i == l) {
66+
found = k
67+
break
68+
}
69+
}
70+
return word.substring(0, found) + result
71+
}
72+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
943\. Find the Shortest Superstring
2+
3+
Hard
4+
5+
Given an array of strings `words`, return _the smallest string that contains each string in_ `words` _as a substring_. If there are multiple valid strings of the smallest length, return **any of them**.
6+
7+
You may assume that no string in `words` is a substring of another string in `words`.
8+
9+
**Example 1:**
10+
11+
**Input:** words = ["alex","loves","leetcode"]
12+
13+
**Output:** "alexlovesleetcode"
14+
15+
**Explanation:** All permutations of "alex","loves","leetcode" would also be accepted.
16+
17+
**Example 2:**
18+
19+
**Input:** words = ["catg","ctaagt","gcta","ttca","atgcatc"]
20+
21+
**Output:** "gctaagttcatgcatc"
22+
23+
**Constraints:**
24+
25+
* `1 <= words.length <= 12`
26+
* `1 <= words[i].length <= 20`
27+
* `words[i]` consists of lowercase English letters.
28+
* All the strings of `words` are **unique**.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0901_1000.s0940_distinct_subsequences_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 distinctSubseqII() {
10+
assertThat(Solution().distinctSubseqII("abc"), equalTo(7))
11+
}
12+
13+
@Test
14+
fun distinctSubseqII2() {
15+
assertThat(Solution().distinctSubseqII("aba"), equalTo(6))
16+
}
17+
18+
@Test
19+
fun distinctSubseqII3() {
20+
assertThat(Solution().distinctSubseqII("aaa"), equalTo(3))
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0901_1000.s0941_valid_mountain_array
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 validMountainArray() {
10+
assertThat(Solution().validMountainArray(intArrayOf(2, 1)), equalTo(false))
11+
}
12+
13+
@Test
14+
fun validMountainArray2() {
15+
assertThat(Solution().validMountainArray(intArrayOf(3, 5, 5)), equalTo(false))
16+
}
17+
18+
@Test
19+
fun validMountainArray3() {
20+
assertThat(Solution().validMountainArray(intArrayOf(0, 3, 2, 1)), equalTo(true))
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0901_1000.s0942_di_string_match
2+
3+
import com_github_leetcode.CommonUtils
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun diStringMatch() {
11+
assertThat(
12+
CommonUtils().compareArray(
13+
Solution().diStringMatch("IDID"), intArrayOf(0, 4, 1, 3, 2)
14+
),
15+
equalTo(true)
16+
)
17+
}
18+
19+
@Test
20+
fun diStringMatch2() {
21+
assertThat(Solution().diStringMatch("III"), equalTo(intArrayOf(0, 1, 2, 3)))
22+
}
23+
24+
@Test
25+
fun diStringMatch3() {
26+
assertThat(Solution().diStringMatch("DDI"), equalTo(intArrayOf(3, 2, 0, 1)))
27+
}
28+
}

0 commit comments

Comments
 (0)