Skip to content

Commit 491efa1

Browse files
authored
Added tasks 3295-3307
1 parent e4457f6 commit 491efa1

File tree

13 files changed

+1314
-215
lines changed
  • src/main/kotlin
    • g3201_3300
      • s3295_report_spam_message
      • s3296_minimum_number_of_seconds_to_make_mountain_height_zero
      • s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i
      • s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii
      • s3300_minimum_element_after_replacement_with_digit_sum
    • g3301_3400
      • s3301_maximize_the_total_height_of_unique_towers
      • s3302_find_the_lexicographically_smallest_valid_sequence
      • s3303_find_the_occurrence_of_first_almost_equal_substring
      • s3304_find_the_k_th_character_in_string_game_i
      • s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i
      • s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii
      • s3307_find_the_k_th_character_in_string_game_ii

13 files changed

+1314
-215
lines changed

README.md

Lines changed: 227 additions & 215 deletions
Large diffs are not rendered by default.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3295\. Report Spam Message
5+
6+
Medium
7+
8+
You are given an array of strings `message` and an array of strings `bannedWords`.
9+
10+
An array of words is considered **spam** if there are **at least** two words in it that **exactly** match any word in `bannedWords`.
11+
12+
Return `true` if the array `message` is spam, and `false` otherwise.
13+
14+
**Example 1:**
15+
16+
**Input:** message = ["hello","world","leetcode"], bannedWords = ["world","hello"]
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
The words `"hello"` and `"world"` from the `message` array both appear in the `bannedWords` array.
23+
24+
**Example 2:**
25+
26+
**Input:** message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"]
27+
28+
**Output:** false
29+
30+
**Explanation:**
31+
32+
Only one word from the `message` array (`"programming"`) appears in the `bannedWords` array.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= message.length, bannedWords.length <= 10<sup>5</sup></code>
37+
* `1 <= message[i].length, bannedWords[i].length <= 15`
38+
* `message[i]` and `bannedWords[i]` consist only of lowercase English letters.
39+
40+
## Solution
41+
42+
```kotlin
43+
class Solution {
44+
fun reportSpam(message: Array<String>, bannedWords: Array<String>): Boolean {
45+
val bannedUnique: MutableSet<String?> = mutableSetOf(*bannedWords)
46+
var bannedCount = 0
47+
for (msg in message) {
48+
if (bannedUnique.contains(msg)) {
49+
bannedCount++
50+
}
51+
if (bannedCount == 2) {
52+
return true
53+
}
54+
}
55+
return false
56+
}
57+
}
58+
```
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3296\. Minimum Number of Seconds to Make Mountain Height Zero
5+
6+
Medium
7+
8+
You are given an integer `mountainHeight` denoting the height of a mountain.
9+
10+
You are also given an integer array `workerTimes` representing the work time of workers in **seconds**.
11+
12+
The workers work **simultaneously** to **reduce** the height of the mountain. For worker `i`:
13+
14+
* To decrease the mountain's height by `x`, it takes `workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x` seconds. For example:
15+
* To reduce the height of the mountain by 1, it takes `workerTimes[i]` seconds.
16+
* To reduce the height of the mountain by 2, it takes `workerTimes[i] + workerTimes[i] * 2` seconds, and so on.
17+
18+
Return an integer representing the **minimum** number of seconds required for the workers to make the height of the mountain 0.
19+
20+
**Example 1:**
21+
22+
**Input:** mountainHeight = 4, workerTimes = [2,1,1]
23+
24+
**Output:** 3
25+
26+
**Explanation:**
27+
28+
One way the height of the mountain can be reduced to 0 is:
29+
30+
* Worker 0 reduces the height by 1, taking `workerTimes[0] = 2` seconds.
31+
* Worker 1 reduces the height by 2, taking `workerTimes[1] + workerTimes[1] * 2 = 3` seconds.
32+
* Worker 2 reduces the height by 1, taking `workerTimes[2] = 1` second.
33+
34+
Since they work simultaneously, the minimum time needed is `max(2, 3, 1) = 3` seconds.
35+
36+
**Example 2:**
37+
38+
**Input:** mountainHeight = 10, workerTimes = [3,2,2,4]
39+
40+
**Output:** 12
41+
42+
**Explanation:**
43+
44+
* Worker 0 reduces the height by 2, taking `workerTimes[0] + workerTimes[0] * 2 = 9` seconds.
45+
* Worker 1 reduces the height by 3, taking `workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12` seconds.
46+
* Worker 2 reduces the height by 3, taking `workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12` seconds.
47+
* Worker 3 reduces the height by 2, taking `workerTimes[3] + workerTimes[3] * 2 = 12` seconds.
48+
49+
The number of seconds needed is `max(9, 12, 12, 12) = 12` seconds.
50+
51+
**Example 3:**
52+
53+
**Input:** mountainHeight = 5, workerTimes = [1]
54+
55+
**Output:** 15
56+
57+
**Explanation:**
58+
59+
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`.
60+
61+
**Constraints:**
62+
63+
* <code>1 <= mountainHeight <= 10<sup>5</sup></code>
64+
* <code>1 <= workerTimes.length <= 10<sup>4</sup></code>
65+
* <code>1 <= workerTimes[i] <= 10<sup>6</sup></code>
66+
67+
## Solution
68+
69+
```kotlin
70+
import kotlin.math.sqrt
71+
72+
class Solution {
73+
fun minNumberOfSeconds(mountainHeight: Int, workerTimes: IntArray): Long {
74+
var left: Long = 0
75+
var right = mountainHeight.toLong() * (mountainHeight + 1) / 2 * workerTimes[0]
76+
while (left < right) {
77+
val mid = left + (right - left) / 2
78+
if (canReduceMountain(workerTimes, mountainHeight, mid)) {
79+
right = mid
80+
} else {
81+
left = mid + 1
82+
}
83+
}
84+
return left
85+
}
86+
87+
private fun canReduceMountain(workerTimes: IntArray, mountainHeight: Int, timeLimit: Long): Boolean {
88+
var totalHeightReduced: Long = 0
89+
for (workerTime in workerTimes) {
90+
val maxHeightThisWorker = (sqrt(2.0 * timeLimit / workerTime + 0.25) - 0.5).toLong()
91+
totalHeightReduced += maxHeightThisWorker
92+
if (totalHeightReduced >= mountainHeight) {
93+
return true
94+
}
95+
}
96+
return totalHeightReduced >= mountainHeight
97+
}
98+
}
99+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3297\. Count Substrings That Can Be Rearranged to Contain a String I
5+
6+
Medium
7+
8+
You are given two strings `word1` and `word2`.
9+
10+
A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix.
11+
12+
Return the total number of **valid** substrings of `word1`.
13+
14+
**Example 1:**
15+
16+
**Input:** word1 = "bcca", word2 = "abc"
17+
18+
**Output:** 1
19+
20+
**Explanation:**
21+
22+
The only valid substring is `"bcca"` which can be rearranged to `"abcc"` having `"abc"` as a prefix.
23+
24+
**Example 2:**
25+
26+
**Input:** word1 = "abcabc", word2 = "abc"
27+
28+
**Output:** 10
29+
30+
**Explanation:**
31+
32+
All the substrings except substrings of size 1 and size 2 are valid.
33+
34+
**Example 3:**
35+
36+
**Input:** word1 = "abcabc", word2 = "aaabc"
37+
38+
**Output:** 0
39+
40+
**Constraints:**
41+
42+
* <code>1 <= word1.length <= 10<sup>5</sup></code>
43+
* <code>1 <= word2.length <= 10<sup>4</sup></code>
44+
* `word1` and `word2` consist only of lowercase English letters.
45+
46+
## Solution
47+
48+
```kotlin
49+
class Solution {
50+
fun validSubstringCount(word1: String, word2: String): Long {
51+
var res: Long = 0
52+
var keys = 0
53+
val len = word1.length
54+
val count = IntArray(26)
55+
val letters = BooleanArray(26)
56+
for (letter in word2.toCharArray()) {
57+
val index = letter.code - 'a'.code
58+
if (count[index]++ == 0) {
59+
letters[index] = true
60+
keys++
61+
}
62+
}
63+
var start = 0
64+
var end = 0
65+
while (end < len) {
66+
val index = word1[end].code - 'a'.code
67+
if (!letters[index]) {
68+
end++
69+
continue
70+
}
71+
if (--count[index] == 0) {
72+
--keys
73+
}
74+
while (keys == 0) {
75+
res += (len - end).toLong()
76+
val beginIndex = word1[start++].code - 'a'.code
77+
if (!letters[beginIndex]) {
78+
continue
79+
}
80+
if (count[beginIndex]++ == 0) {
81+
keys++
82+
}
83+
}
84+
end++
85+
}
86+
return res
87+
}
88+
}
89+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3298\. Count Substrings That Can Be Rearranged to Contain a String II
5+
6+
Hard
7+
8+
You are given two strings `word1` and `word2`.
9+
10+
A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix.
11+
12+
Return the total number of **valid** substrings of `word1`.
13+
14+
**Note** that the memory limits in this problem are **smaller** than usual, so you **must** implement a solution with a _linear_ runtime complexity.
15+
16+
**Example 1:**
17+
18+
**Input:** word1 = "bcca", word2 = "abc"
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
The only valid substring is `"bcca"` which can be rearranged to `"abcc"` having `"abc"` as a prefix.
25+
26+
**Example 2:**
27+
28+
**Input:** word1 = "abcabc", word2 = "abc"
29+
30+
**Output:** 10
31+
32+
**Explanation:**
33+
34+
All the substrings except substrings of size 1 and size 2 are valid.
35+
36+
**Example 3:**
37+
38+
**Input:** word1 = "abcabc", word2 = "aaabc"
39+
40+
**Output:** 0
41+
42+
**Constraints:**
43+
44+
* <code>1 <= word1.length <= 10<sup>6</sup></code>
45+
* <code>1 <= word2.length <= 10<sup>4</sup></code>
46+
* `word1` and `word2` consist only of lowercase English letters.
47+
48+
## Solution
49+
50+
```kotlin
51+
class Solution {
52+
fun validSubstringCount(word1: String, word2: String): Long {
53+
val ar = word1.toCharArray()
54+
val n = ar.size
55+
val temp = word2.toCharArray()
56+
val f = IntArray(26)
57+
for (i in temp) {
58+
f[i.code - 97]++
59+
}
60+
var ans: Long = 0
61+
var needed = temp.size
62+
var beg = 0
63+
var end = 0
64+
while (end < n) {
65+
if (f[ar[end].code - 97]-- > 0) {
66+
needed--
67+
}
68+
while (needed == 0) {
69+
// All substrings from [beg, i], where end <= i < n are valid
70+
ans += (n - end).toLong()
71+
// Shrink
72+
if (f[ar[beg++].code - 97]++ == 0) {
73+
needed++
74+
}
75+
}
76+
end++
77+
}
78+
return ans
79+
}
80+
}
81+
```

0 commit comments

Comments
 (0)