Skip to content

Commit a171823

Browse files
authored
Added tasks 3295-3298
1 parent 76688f9 commit a171823

File tree

12 files changed

+438
-0
lines changed

12 files changed

+438
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3201_3300.s3295_report_spam_message
2+
3+
// #Medium #Array #String #Hash_Table #2024_09_24_Time_782_ms_(74.19%)_Space_109.6_MB_(38.71%)
4+
5+
class Solution {
6+
fun reportSpam(message: Array<String>, bannedWords: Array<String>): Boolean {
7+
val bannedUnique: MutableSet<String?> = mutableSetOf(*bannedWords)
8+
var bannedCount = 0
9+
for (msg in message) {
10+
if (bannedUnique.contains(msg)) {
11+
bannedCount++
12+
}
13+
if (bannedCount == 2) {
14+
return true
15+
}
16+
}
17+
return false
18+
}
19+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
3295\. Report Spam Message
2+
3+
Medium
4+
5+
You are given an array of strings `message` and an array of strings `bannedWords`.
6+
7+
An array of words is considered **spam** if there are **at least** two words in it that **exactly** match any word in `bannedWords`.
8+
9+
Return `true` if the array `message` is spam, and `false` otherwise.
10+
11+
**Example 1:**
12+
13+
**Input:** message = ["hello","world","leetcode"], bannedWords = ["world","hello"]
14+
15+
**Output:** true
16+
17+
**Explanation:**
18+
19+
The words `"hello"` and `"world"` from the `message` array both appear in the `bannedWords` array.
20+
21+
**Example 2:**
22+
23+
**Input:** message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]
24+
25+
**Output:** false
26+
27+
**Explanation:**
28+
29+
Only one word from the `message` array (`"programming"`) appears in the `bannedWords` array.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code>
34+
* `1 <= message[i].length, bannedWords[i].length <= 15`
35+
* `message[i]` and `bannedWords[i]` consist only of lowercase English letters.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero
2+
3+
// #Medium #Array #Math #Binary_Search #2024_09_24_Time_228_ms_(87.50%)_Space_38.6_MB_(81.25%)
4+
5+
import kotlin.math.sqrt
6+
7+
class Solution {
8+
fun minNumberOfSeconds(mountainHeight: Int, workerTimes: IntArray): Long {
9+
var left: Long = 0
10+
var right = mountainHeight.toLong() * (mountainHeight + 1) / 2 * workerTimes[0]
11+
while (left < right) {
12+
val mid = left + (right - left) / 2
13+
if (canReduceMountain(workerTimes, mountainHeight, mid)) {
14+
right = mid
15+
} else {
16+
left = mid + 1
17+
}
18+
}
19+
return left
20+
}
21+
22+
private fun canReduceMountain(workerTimes: IntArray, mountainHeight: Int, timeLimit: Long): Boolean {
23+
var totalHeightReduced: Long = 0
24+
for (workerTime in workerTimes) {
25+
val maxHeightThisWorker = (sqrt(2.0 * timeLimit / workerTime + 0.25) - 0.5).toLong()
26+
totalHeightReduced += maxHeightThisWorker
27+
if (totalHeightReduced >= mountainHeight) {
28+
return true
29+
}
30+
}
31+
return totalHeightReduced >= mountainHeight
32+
}
33+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3296\. Minimum Number of Seconds to Make Mountain Height Zero
2+
3+
Medium
4+
5+
You are given an integer `mountainHeight` denoting the height of a mountain.
6+
7+
You are also given an integer array `workerTimes` representing the work time of workers in **seconds**.
8+
9+
The workers work **simultaneously** to **reduce** the height of the mountain. For worker `i`:
10+
11+
* To decrease the mountain's height by `x`, it takes `workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x` seconds. For example:
12+
* To reduce the height of the mountain by 1, it takes `workerTimes[i]` seconds.
13+
* To reduce the height of the mountain by 2, it takes `workerTimes[i] + workerTimes[i] * 2` seconds, and so on.
14+
15+
Return an integer representing the **minimum** number of seconds required for the workers to make the height of the mountain 0.
16+
17+
**Example 1:**
18+
19+
**Input:** mountainHeight = 4, workerTimes = [2,1,1]
20+
21+
**Output:** 3
22+
23+
**Explanation:**
24+
25+
One way the height of the mountain can be reduced to 0 is:
26+
27+
* Worker 0 reduces the height by 1, taking `workerTimes[0] = 2` seconds.
28+
* Worker 1 reduces the height by 2, taking `workerTimes[1] + workerTimes[1] * 2 = 3` seconds.
29+
* Worker 2 reduces the height by 1, taking `workerTimes[2] = 1` second.
30+
31+
Since they work simultaneously, the minimum time needed is `max(2, 3, 1) = 3` seconds.
32+
33+
**Example 2:**
34+
35+
**Input:** mountainHeight = 10, workerTimes = [3,2,2,4]
36+
37+
**Output:** 12
38+
39+
**Explanation:**
40+
41+
* Worker 0 reduces the height by 2, taking `workerTimes[0] + workerTimes[0] * 2 = 9` seconds.
42+
* Worker 1 reduces the height by 3, taking `workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12` seconds.
43+
* Worker 2 reduces the height by 3, taking `workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12` seconds.
44+
* Worker 3 reduces the height by 2, taking `workerTimes[3] + workerTimes[3] * 2 = 12` seconds.
45+
46+
The number of seconds needed is `max(9, 12, 12, 12) = 12` seconds.
47+
48+
**Example 3:**
49+
50+
**Input:** mountainHeight = 5, workerTimes = [1]
51+
52+
**Output:** 15
53+
54+
**Explanation:**
55+
56+
There is only one worker in this example, so the answer is `workerTimes[0] + workerTimes[0] * 2 + workerTimes[0] * 3 + workerTimes[0] * 4 + workerTimes[0] * 5 = 15`.
57+
58+
**Constraints:**
59+
60+
* <code>1 <= mountainHeight <= 10<sup>5</sup></code>
61+
* <code>1 <= workerTimes.length <= 10<sup>4</sup></code>
62+
* <code>1 <= workerTimes[i] <= 10<sup>6</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g3201_3300.s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i
2+
3+
// #Medium #String #Hash_Table #Sliding_Window
4+
// #2024_09_24_Time_215_ms_(93.33%)_Space_38.2_MB_(93.33%)
5+
6+
class Solution {
7+
fun validSubstringCount(word1: String, word2: String): Long {
8+
var res: Long = 0
9+
var keys = 0
10+
val len = word1.length
11+
val count = IntArray(26)
12+
val letters = BooleanArray(26)
13+
for (letter in word2.toCharArray()) {
14+
val index = letter.code - 'a'.code
15+
if (count[index]++ == 0) {
16+
letters[index] = true
17+
keys++
18+
}
19+
}
20+
var start = 0
21+
var end = 0
22+
while (end < len) {
23+
val index = word1[end].code - 'a'.code
24+
if (!letters[index]) {
25+
end++
26+
continue
27+
}
28+
if (--count[index] == 0) {
29+
--keys
30+
}
31+
while (keys == 0) {
32+
res += (len - end).toLong()
33+
val beginIndex = word1[start++].code - 'a'.code
34+
if (!letters[beginIndex]) {
35+
continue
36+
}
37+
if (count[beginIndex]++ == 0) {
38+
keys++
39+
}
40+
}
41+
end++
42+
}
43+
return res
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3297\. Count Substrings That Can Be Rearranged to Contain a String I
2+
3+
Medium
4+
5+
You are given two strings `word1` and `word2`.
6+
7+
A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix.
8+
9+
Return the total number of **valid** substrings of `word1`.
10+
11+
**Example 1:**
12+
13+
**Input:** word1 = "bcca", word2 = "abc"
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
The only valid substring is `"bcca"` which can be rearranged to `"abcc"` having `"abc"` as a prefix.
20+
21+
**Example 2:**
22+
23+
**Input:** word1 = "abcabc", word2 = "abc"
24+
25+
**Output:** 10
26+
27+
**Explanation:**
28+
29+
All the substrings except substrings of size 1 and size 2 are valid.
30+
31+
**Example 3:**
32+
33+
**Input:** word1 = "abcabc", word2 = "aaabc"
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>1 <= word1.length <= 10<sup>5</sup></code>
40+
* <code>1 <= word2.length <= 10<sup>4</sup></code>
41+
* `word1` and `word2` consist only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3201_3300.s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii
2+
3+
// #Hard #String #Hash_Table #Sliding_Window #2024_09_24_Time_433_ms_(78.57%)_Space_51.9_MB_(42.86%)
4+
5+
class Solution {
6+
fun validSubstringCount(word1: String, word2: String): Long {
7+
val ar = word1.toCharArray()
8+
val n = ar.size
9+
val temp = word2.toCharArray()
10+
val f = IntArray(26)
11+
for (i in temp) {
12+
f[i.code - 97]++
13+
}
14+
var ans: Long = 0
15+
var needed = temp.size
16+
var beg = 0
17+
var end = 0
18+
while (end < n) {
19+
if (f[ar[end].code - 97]-- > 0) {
20+
needed--
21+
}
22+
while (needed == 0) {
23+
// All substrings from [beg, i], where end <= i < n are valid
24+
ans += (n - end).toLong()
25+
// Shrink
26+
if (f[ar[beg++].code - 97]++ == 0) {
27+
needed++
28+
}
29+
}
30+
end++
31+
}
32+
return ans
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3298\. Count Substrings That Can Be Rearranged to Contain a String II
2+
3+
Hard
4+
5+
You are given two strings `word1` and `word2`.
6+
7+
A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix.
8+
9+
Return the total number of **valid** substrings of `word1`.
10+
11+
**Note** that the memory limits in this problem are **smaller** than usual, so you **must** implement a solution with a _linear_ runtime complexity.
12+
13+
**Example 1:**
14+
15+
**Input:** word1 = "bcca", word2 = "abc"
16+
17+
**Output:** 1
18+
19+
**Explanation:**
20+
21+
The only valid substring is `"bcca"` which can be rearranged to `"abcc"` having `"abc"` as a prefix.
22+
23+
**Example 2:**
24+
25+
**Input:** word1 = "abcabc", word2 = "abc"
26+
27+
**Output:** 10
28+
29+
**Explanation:**
30+
31+
All the substrings except substrings of size 1 and size 2 are valid.
32+
33+
**Example 3:**
34+
35+
**Input:** word1 = "abcabc", word2 = "aaabc"
36+
37+
**Output:** 0
38+
39+
**Constraints:**
40+
41+
* <code>1 <= word1.length <= 10<sup>6</sup></code>
42+
* <code>1 <= word2.length <= 10<sup>4</sup></code>
43+
* `word1` and `word2` consist only of lowercase English letters.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3201_3300.s3295_report_spam_message
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 reportSpam() {
10+
assertThat<Boolean>(
11+
Solution()
12+
.reportSpam(
13+
arrayOf<String>("hello", "world", "leetcode"),
14+
arrayOf<String>("world", "hello")
15+
),
16+
equalTo<Boolean>(true)
17+
)
18+
}
19+
20+
@Test
21+
fun reportSpam2() {
22+
assertThat<Boolean>(
23+
Solution()
24+
.reportSpam(
25+
arrayOf<String>("hello", "programming", "fun"),
26+
arrayOf<String>("world", "programming", "leetcode")
27+
),
28+
equalTo<Boolean>(false)
29+
)
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero
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 minNumberOfSeconds() {
10+
assertThat<Long>(
11+
Solution().minNumberOfSeconds(4, intArrayOf(2, 1, 1)),
12+
equalTo<Long>(3L)
13+
)
14+
}
15+
16+
@Test
17+
fun minNumberOfSeconds2() {
18+
assertThat<Long>(
19+
Solution().minNumberOfSeconds(10, intArrayOf(3, 2, 2, 4)),
20+
equalTo<Long>(12L)
21+
)
22+
}
23+
24+
@Test
25+
fun minNumberOfSeconds3() {
26+
assertThat<Long>(
27+
Solution().minNumberOfSeconds(5, intArrayOf(1)),
28+
equalTo<Long>(15L)
29+
)
30+
}
31+
}

0 commit comments

Comments
 (0)