Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f69942d

Browse files
authored
Added tasks 3295-3298
1 parent d9c7bc4 commit f69942d

File tree

12 files changed

+411
-0
lines changed

12 files changed

+411
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3201_3300.s3295_report_spam_message;
2+
3+
// #Medium #Array #String #Hash_Table #2024_09_24_Time_39_ms_(98.87%)_Space_85.4_MB_(9.13%)
4+
5+
import java.util.Arrays;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class Solution {
10+
public boolean reportSpam(String[] message, String[] bannedWords) {
11+
Set<String> bannedUnique = new HashSet<>(Arrays.asList(bannedWords));
12+
int bannedCount = 0;
13+
for (String msg : message) {
14+
if (bannedUnique.contains(msg)) {
15+
bannedCount++;
16+
}
17+
if (bannedCount == 2) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
}
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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_6_ms_(100.00%)_Space_45.2_MB_(90.23%)
4+
5+
public class Solution {
6+
public long minNumberOfSeconds(int mountainHeight, int[] workerTimes) {
7+
long left = 0;
8+
long right = (long) mountainHeight * (mountainHeight + 1) / 2 * workerTimes[0];
9+
while (left < right) {
10+
long mid = left + (right - left) / 2;
11+
if (canReduceMountain(workerTimes, mountainHeight, mid)) {
12+
right = mid;
13+
} else {
14+
left = mid + 1;
15+
}
16+
}
17+
return left;
18+
}
19+
20+
private boolean canReduceMountain(int[] workerTimes, int mountainHeight, long timeLimit) {
21+
long totalHeightReduced = 0;
22+
for (int workerTime : workerTimes) {
23+
long maxHeightThisWorker =
24+
(long) (Math.sqrt(2.0 * timeLimit / workerTime + 0.25) - 0.5);
25+
totalHeightReduced += maxHeightThisWorker;
26+
if (totalHeightReduced >= mountainHeight) {
27+
return true;
28+
}
29+
}
30+
return totalHeightReduced >= mountainHeight;
31+
}
32+
}
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>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g3201_3300.s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i;
2+
3+
// #Medium #String #Hash_Table #Sliding_Window #2024_09_24_Time_5_ms_(99.40%)_Space_44.9_MB_(51.04%)
4+
5+
public class Solution {
6+
public long validSubstringCount(String word1, String word2) {
7+
long res = 0;
8+
int keys = 0;
9+
int len = word1.length();
10+
int[] count = new int[26];
11+
boolean[] letters = new boolean[26];
12+
for (char letter : word2.toCharArray()) {
13+
int index = letter - 'a';
14+
if (count[index]++ == 0) {
15+
letters[index] = true;
16+
keys++;
17+
}
18+
}
19+
int start = 0;
20+
int end = 0;
21+
while (end < len) {
22+
int index = word1.charAt(end) - 'a';
23+
if (!letters[index]) {
24+
end++;
25+
continue;
26+
}
27+
if (--count[index] == 0) {
28+
--keys;
29+
}
30+
while (keys == 0) {
31+
res += len - end;
32+
int beginIndex = word1.charAt(start++) - 'a';
33+
if (!letters[beginIndex]) {
34+
continue;
35+
}
36+
if (count[beginIndex]++ == 0) {
37+
keys++;
38+
}
39+
}
40+
end++;
41+
}
42+
return res;
43+
}
44+
}
Lines changed: 41 additions & 0 deletions
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.
Lines changed: 34 additions & 0 deletions
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_31_ms_(100.00%)_Space_56.1_MB_(68.76%)
4+
5+
public class Solution {
6+
public long validSubstringCount(String word1, String word2) {
7+
char[] ar = word1.toCharArray();
8+
int n = ar.length;
9+
char[] temp = word2.toCharArray();
10+
int[] f = new int[26];
11+
for (char i : temp) {
12+
f[i - 97]++;
13+
}
14+
long ans = 0;
15+
int needed = temp.length;
16+
int beg = 0;
17+
int end = 0;
18+
while (end < n) {
19+
if (f[ar[end] - 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;
25+
// Shrink
26+
if (f[ar[beg++] - 97]++ == 0) {
27+
needed++;
28+
}
29+
}
30+
end++;
31+
}
32+
return ans;
33+
}
34+
}
Lines changed: 43 additions & 0 deletions
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3201_3300.s3295_report_spam_message;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void reportSpam() {
11+
assertThat(
12+
new Solution()
13+
.reportSpam(
14+
new String[] {"hello", "world", "leetcode"},
15+
new String[] {"world", "hello"}),
16+
equalTo(true));
17+
}
18+
19+
@Test
20+
void reportSpam2() {
21+
assertThat(
22+
new Solution()
23+
.reportSpam(
24+
new String[] {"hello", "programming", "fun"},
25+
new String[] {"world", "programming", "leetcode"}),
26+
equalTo(false));
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minNumberOfSeconds() {
11+
assertThat(new Solution().minNumberOfSeconds(4, new int[] {2, 1, 1}), equalTo(3L));
12+
}
13+
14+
@Test
15+
void minNumberOfSeconds2() {
16+
assertThat(new Solution().minNumberOfSeconds(10, new int[] {3, 2, 2, 4}), equalTo(12L));
17+
}
18+
19+
@Test
20+
void minNumberOfSeconds3() {
21+
assertThat(new Solution().minNumberOfSeconds(5, new int[] {1}), equalTo(15L));
22+
}
23+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /