Skip to content

Commit 64ae546

Browse files
authored
Added tasks 3295-3307
1 parent ab0d26a commit 64ae546

File tree

13 files changed

+1315
-216
lines changed
  • src/main/java
    • 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

+1315
-216
lines changed

README.md

Lines changed: 228 additions & 216 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
43+
import java.util.Arrays;
44+
import java.util.HashSet;
45+
import java.util.Set;
46+
47+
public class Solution {
48+
public boolean reportSpam(String[] message, String[] bannedWords) {
49+
Set<String> bannedUnique = new HashSet<>(Arrays.asList(bannedWords));
50+
int bannedCount = 0;
51+
for (String msg : message) {
52+
if (bannedUnique.contains(msg)) {
53+
bannedCount++;
54+
}
55+
if (bannedCount == 2) {
56+
return true;
57+
}
58+
}
59+
return false;
60+
}
61+
}
62+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
70+
public class Solution {
71+
public long minNumberOfSeconds(int mountainHeight, int[] workerTimes) {
72+
long left = 0;
73+
long right = (long) mountainHeight * (mountainHeight + 1) / 2 * workerTimes[0];
74+
while (left < right) {
75+
long mid = left + (right - left) / 2;
76+
if (canReduceMountain(workerTimes, mountainHeight, mid)) {
77+
right = mid;
78+
} else {
79+
left = mid + 1;
80+
}
81+
}
82+
return left;
83+
}
84+
85+
private boolean canReduceMountain(int[] workerTimes, int mountainHeight, long timeLimit) {
86+
long totalHeightReduced = 0;
87+
for (int workerTime : workerTimes) {
88+
long maxHeightThisWorker =
89+
(long) (Math.sqrt(2.0 * timeLimit / workerTime + 0.25) - 0.5);
90+
totalHeightReduced += maxHeightThisWorker;
91+
if (totalHeightReduced >= mountainHeight) {
92+
return true;
93+
}
94+
}
95+
return totalHeightReduced >= mountainHeight;
96+
}
97+
}
98+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
49+
public class Solution {
50+
public long validSubstringCount(String word1, String word2) {
51+
long res = 0;
52+
int keys = 0;
53+
int len = word1.length();
54+
int[] count = new int[26];
55+
boolean[] letters = new boolean[26];
56+
for (char letter : word2.toCharArray()) {
57+
int index = letter - 'a';
58+
if (count[index]++ == 0) {
59+
letters[index] = true;
60+
keys++;
61+
}
62+
}
63+
int start = 0;
64+
int end = 0;
65+
while (end < len) {
66+
int index = word1.charAt(end) - 'a';
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;
76+
int beginIndex = word1.charAt(start++) - 'a';
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+
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
51+
public class Solution {
52+
public long validSubstringCount(String word1, String word2) {
53+
char[] ar = word1.toCharArray();
54+
int n = ar.length;
55+
char[] temp = word2.toCharArray();
56+
int[] f = new int[26];
57+
for (char i : temp) {
58+
f[i - 97]++;
59+
}
60+
long ans = 0;
61+
int needed = temp.length;
62+
int beg = 0;
63+
int end = 0;
64+
while (end < n) {
65+
if (f[ar[end] - 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;
71+
// Shrink
72+
if (f[ar[beg++] - 97]++ == 0) {
73+
needed++;
74+
}
75+
}
76+
end++;
77+
}
78+
return ans;
79+
}
80+
}
81+
```

0 commit comments

Comments
 (0)