diff --git a/src/main/java/g3201_3300/s3295_report_spam_message/Solution.java b/src/main/java/g3201_3300/s3295_report_spam_message/Solution.java new file mode 100644 index 000000000..b494eeeab --- /dev/null +++ b/src/main/java/g3201_3300/s3295_report_spam_message/Solution.java @@ -0,0 +1,23 @@ +package g3201_3300.s3295_report_spam_message; + +// #Medium #Array #String #Hash_Table #2024_09_24_Time_39_ms_(98.87%)_Space_85.4_MB_(9.13%) + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class Solution { + public boolean reportSpam(String[] message, String[] bannedWords) { + Set bannedUnique = new HashSet<>(Arrays.asList(bannedWords)); + int bannedCount = 0; + for (String msg : message) { + if (bannedUnique.contains(msg)) { + bannedCount++; + } + if (bannedCount == 2) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/g3201_3300/s3295_report_spam_message/readme.md b/src/main/java/g3201_3300/s3295_report_spam_message/readme.md new file mode 100644 index 000000000..8b12c226f --- /dev/null +++ b/src/main/java/g3201_3300/s3295_report_spam_message/readme.md @@ -0,0 +1,35 @@ +3295\. Report Spam Message + +Medium + +You are given an array of strings `message` and an array of strings `bannedWords`. + +An array of words is considered **spam** if there are **at least** two words in it that **exactly** match any word in `bannedWords`. + +Return `true` if the array `message` is spam, and `false` otherwise. + +**Example 1:** + +**Input:** message = ["hello","world","leetcode"], bannedWords = ["world","hello"] + +**Output:** true + +**Explanation:** + +The words `"hello"` and `"world"` from the `message` array both appear in the `bannedWords` array. + +**Example 2:** + +**Input:** message = ["hello","programming","fun"], bannedWords = ["world","programming","leetcode"] + +**Output:** false + +**Explanation:** + +Only one word from the `message` array (`"programming"`) appears in the `bannedWords` array. + +**Constraints:** + +* 1 <= message.length, bannedWords.length <= 105 +* `1 <= message[i].length, bannedWords[i].length <= 15` +* `message[i]` and `bannedWords[i]` consist only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/Solution.java b/src/main/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/Solution.java new file mode 100644 index 000000000..72937680d --- /dev/null +++ b/src/main/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/Solution.java @@ -0,0 +1,32 @@ +package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero; + +// #Medium #Array #Math #Binary_Search #2024_09_24_Time_6_ms_(100.00%)_Space_45.2_MB_(90.23%) + +public class Solution { + public long minNumberOfSeconds(int mountainHeight, int[] workerTimes) { + long left = 0; + long right = (long) mountainHeight * (mountainHeight + 1) / 2 * workerTimes[0]; + while (left < right) { + long mid = left + (right - left) / 2; + if (canReduceMountain(workerTimes, mountainHeight, mid)) { + right = mid; + } else { + left = mid + 1; + } + } + return left; + } + + private boolean canReduceMountain(int[] workerTimes, int mountainHeight, long timeLimit) { + long totalHeightReduced = 0; + for (int workerTime : workerTimes) { + long maxHeightThisWorker = + (long) (Math.sqrt(2.0 * timeLimit / workerTime + 0.25) - 0.5); + totalHeightReduced += maxHeightThisWorker; + if (totalHeightReduced >= mountainHeight) { + return true; + } + } + return totalHeightReduced >= mountainHeight; + } +} diff --git a/src/main/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/readme.md b/src/main/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/readme.md new file mode 100644 index 000000000..2b6af89d0 --- /dev/null +++ b/src/main/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/readme.md @@ -0,0 +1,62 @@ +3296\. Minimum Number of Seconds to Make Mountain Height Zero + +Medium + +You are given an integer `mountainHeight` denoting the height of a mountain. + +You are also given an integer array `workerTimes` representing the work time of workers in **seconds**. + +The workers work **simultaneously** to **reduce** the height of the mountain. For worker `i`: + +* To decrease the mountain's height by `x`, it takes `workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x` seconds. For example: + * To reduce the height of the mountain by 1, it takes `workerTimes[i]` seconds. + * To reduce the height of the mountain by 2, it takes `workerTimes[i] + workerTimes[i] * 2` seconds, and so on. + +Return an integer representing the **minimum** number of seconds required for the workers to make the height of the mountain 0. + +**Example 1:** + +**Input:** mountainHeight = 4, workerTimes = [2,1,1] + +**Output:** 3 + +**Explanation:** + +One way the height of the mountain can be reduced to 0 is: + +* Worker 0 reduces the height by 1, taking `workerTimes[0] = 2` seconds. +* Worker 1 reduces the height by 2, taking `workerTimes[1] + workerTimes[1] * 2 = 3` seconds. +* Worker 2 reduces the height by 1, taking `workerTimes[2] = 1` second. + +Since they work simultaneously, the minimum time needed is `max(2, 3, 1) = 3` seconds. + +**Example 2:** + +**Input:** mountainHeight = 10, workerTimes = [3,2,2,4] + +**Output:** 12 + +**Explanation:** + +* Worker 0 reduces the height by 2, taking `workerTimes[0] + workerTimes[0] * 2 = 9` seconds. +* Worker 1 reduces the height by 3, taking `workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12` seconds. +* Worker 2 reduces the height by 3, taking `workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12` seconds. +* Worker 3 reduces the height by 2, taking `workerTimes[3] + workerTimes[3] * 2 = 12` seconds. + +The number of seconds needed is `max(9, 12, 12, 12) = 12` seconds. + +**Example 3:** + +**Input:** mountainHeight = 5, workerTimes = [1] + +**Output:** 15 + +**Explanation:** + +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`. + +**Constraints:** + +* 1 <= mountainHeight <= 105 +* 1 <= workerTimes.length <= 104 +* 1 <= workerTimes[i] <= 106 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/Solution.java b/src/main/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/Solution.java new file mode 100644 index 000000000..b3df92446 --- /dev/null +++ b/src/main/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/Solution.java @@ -0,0 +1,44 @@ +package g3201_3300.s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i; + +// #Medium #String #Hash_Table #Sliding_Window #2024_09_24_Time_5_ms_(99.40%)_Space_44.9_MB_(51.04%) + +public class Solution { + public long validSubstringCount(String word1, String word2) { + long res = 0; + int keys = 0; + int len = word1.length(); + int[] count = new int[26]; + boolean[] letters = new boolean[26]; + for (char letter : word2.toCharArray()) { + int index = letter - 'a'; + if (count[index]++ == 0) { + letters[index] = true; + keys++; + } + } + int start = 0; + int end = 0; + while (end < len) { + int index = word1.charAt(end) - 'a'; + if (!letters[index]) { + end++; + continue; + } + if (--count[index] == 0) { + --keys; + } + while (keys == 0) { + res += len - end; + int beginIndex = word1.charAt(start++) - 'a'; + if (!letters[beginIndex]) { + continue; + } + if (count[beginIndex]++ == 0) { + keys++; + } + } + end++; + } + return res; + } +} diff --git a/src/main/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/readme.md b/src/main/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/readme.md new file mode 100644 index 000000000..59ffeb4b1 --- /dev/null +++ b/src/main/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/readme.md @@ -0,0 +1,41 @@ +3297\. Count Substrings That Can Be Rearranged to Contain a String I + +Medium + +You are given two strings `word1` and `word2`. + +A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix. + +Return the total number of **valid** substrings of `word1`. + +**Example 1:** + +**Input:** word1 = "bcca", word2 = "abc" + +**Output:** 1 + +**Explanation:** + +The only valid substring is `"bcca"` which can be rearranged to `"abcc"` having `"abc"` as a prefix. + +**Example 2:** + +**Input:** word1 = "abcabc", word2 = "abc" + +**Output:** 10 + +**Explanation:** + +All the substrings except substrings of size 1 and size 2 are valid. + +**Example 3:** + +**Input:** word1 = "abcabc", word2 = "aaabc" + +**Output:** 0 + +**Constraints:** + +* 1 <= word1.length <= 105 +* 1 <= word2.length <= 104 +* `word1` and `word2` consist only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/Solution.java b/src/main/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/Solution.java new file mode 100644 index 000000000..d90e0ae4d --- /dev/null +++ b/src/main/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/Solution.java @@ -0,0 +1,34 @@ +package g3201_3300.s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii; + +// #Hard #String #Hash_Table #Sliding_Window #2024_09_24_Time_31_ms_(100.00%)_Space_56.1_MB_(68.76%) + +public class Solution { + public long validSubstringCount(String word1, String word2) { + char[] ar = word1.toCharArray(); + int n = ar.length; + char[] temp = word2.toCharArray(); + int[] f = new int[26]; + for (char i : temp) { + f[i - 97]++; + } + long ans = 0; + int needed = temp.length; + int beg = 0; + int end = 0; + while (end < n) { + if (f[ar[end] - 97]-- > 0) { + needed--; + } + while (needed == 0) { + // All substrings from [beg, i], where end <= i < n are valid + ans += n - end; + // Shrink + if (f[ar[beg++] - 97]++ == 0) { + needed++; + } + } + end++; + } + return ans; + } +} diff --git a/src/main/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/readme.md b/src/main/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/readme.md new file mode 100644 index 000000000..510e437e6 --- /dev/null +++ b/src/main/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/readme.md @@ -0,0 +1,43 @@ +3298\. Count Substrings That Can Be Rearranged to Contain a String II + +Hard + +You are given two strings `word1` and `word2`. + +A string `x` is called **valid** if `x` can be rearranged to have `word2` as a prefix. + +Return the total number of **valid** substrings of `word1`. + +**Note** that the memory limits in this problem are **smaller** than usual, so you **must** implement a solution with a _linear_ runtime complexity. + +**Example 1:** + +**Input:** word1 = "bcca", word2 = "abc" + +**Output:** 1 + +**Explanation:** + +The only valid substring is `"bcca"` which can be rearranged to `"abcc"` having `"abc"` as a prefix. + +**Example 2:** + +**Input:** word1 = "abcabc", word2 = "abc" + +**Output:** 10 + +**Explanation:** + +All the substrings except substrings of size 1 and size 2 are valid. + +**Example 3:** + +**Input:** word1 = "abcabc", word2 = "aaabc" + +**Output:** 0 + +**Constraints:** + +* 1 <= word1.length <= 106 +* 1 <= word2.length <= 104 +* `word1` and `word2` consist only of lowercase English letters. \ No newline at end of file diff --git a/src/test/java/g3201_3300/s3295_report_spam_message/SolutionTest.java b/src/test/java/g3201_3300/s3295_report_spam_message/SolutionTest.java new file mode 100644 index 000000000..68ce972e3 --- /dev/null +++ b/src/test/java/g3201_3300/s3295_report_spam_message/SolutionTest.java @@ -0,0 +1,28 @@ +package g3201_3300.s3295_report_spam_message; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void reportSpam() { + assertThat( + new Solution() + .reportSpam( + new String[] {"hello", "world", "leetcode"}, + new String[] {"world", "hello"}), + equalTo(true)); + } + + @Test + void reportSpam2() { + assertThat( + new Solution() + .reportSpam( + new String[] {"hello", "programming", "fun"}, + new String[] {"world", "programming", "leetcode"}), + equalTo(false)); + } +} diff --git a/src/test/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/SolutionTest.java b/src/test/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/SolutionTest.java new file mode 100644 index 000000000..cf2a05dd5 --- /dev/null +++ b/src/test/java/g3201_3300/s3296_minimum_number_of_seconds_to_make_mountain_height_zero/SolutionTest.java @@ -0,0 +1,23 @@ +package g3201_3300.s3296_minimum_number_of_seconds_to_make_mountain_height_zero; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minNumberOfSeconds() { + assertThat(new Solution().minNumberOfSeconds(4, new int[] {2, 1, 1}), equalTo(3L)); + } + + @Test + void minNumberOfSeconds2() { + assertThat(new Solution().minNumberOfSeconds(10, new int[] {3, 2, 2, 4}), equalTo(12L)); + } + + @Test + void minNumberOfSeconds3() { + assertThat(new Solution().minNumberOfSeconds(5, new int[] {1}), equalTo(15L)); + } +} diff --git a/src/test/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/SolutionTest.java b/src/test/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/SolutionTest.java new file mode 100644 index 000000000..294f98e5a --- /dev/null +++ b/src/test/java/g3201_3300/s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3201_3300.s3297_count_substrings_that_can_be_rearranged_to_contain_a_string_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void validSubstringCount() { + assertThat(new Solution().validSubstringCount("bcca", "abc"), equalTo(1L)); + } + + @Test + void validSubstringCount2() { + assertThat(new Solution().validSubstringCount("abcabc", "abc"), equalTo(10L)); + } + + @Test + void validSubstringCount3() { + assertThat(new Solution().validSubstringCount("abcabc", "aaabc"), equalTo(0L)); + } +} diff --git a/src/test/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/SolutionTest.java b/src/test/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/SolutionTest.java new file mode 100644 index 000000000..e37149fac --- /dev/null +++ b/src/test/java/g3201_3300/s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3201_3300.s3298_count_substrings_that_can_be_rearranged_to_contain_a_string_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void validSubstringCount() { + assertThat(new Solution().validSubstringCount("bcca", "abc"), equalTo(1L)); + } + + @Test + void validSubstringCount2() { + assertThat(new Solution().validSubstringCount("abcabc", "abc"), equalTo(10L)); + } + + @Test + void validSubstringCount3() { + assertThat(new Solution().validSubstringCount("abcabc", "aaabc"), equalTo(0L)); + } +}