From 63a38bddf52f6102acb83c9f875637c852f2ffc6 Mon Sep 17 00:00:00 2001 From: ThanhNIT <93962044+ThanhNIT@users.noreply.github.com> Date: Fri, 1 Mar 2024 22:38:42 +0700 Subject: [PATCH] Added tasks 3031-3036 --- .../Solution.java | 33 ++++++++++++ .../readme.md | 50 +++++++++++++++++ .../s3033_modify_the_matrix/Solution.java | 22 ++++++++ .../s3033_modify_the_matrix/readme.md | 37 +++++++++++++ .../Solution.java | 30 +++++++++++ .../readme.md | 37 +++++++++++++ .../Solution.java | 51 ++++++++++++++++++ .../readme.md | 53 +++++++++++++++++++ .../Solution.java | 38 +++++++++++++ .../readme.md | 36 +++++++++++++ .../SolutionTest.java | 23 ++++++++ .../s3033_modify_the_matrix/SolutionTest.java | 29 ++++++++++ .../SolutionTest.java | 25 +++++++++ .../SolutionTest.java | 29 ++++++++++ .../SolutionTest.java | 25 +++++++++ 15 files changed, 518 insertions(+) create mode 100644 src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/Solution.java create mode 100644 src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/readme.md create mode 100644 src/main/java/g3001_3100/s3033_modify_the_matrix/Solution.java create mode 100644 src/main/java/g3001_3100/s3033_modify_the_matrix/readme.md create mode 100644 src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/Solution.java create mode 100644 src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/readme.md create mode 100644 src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/Solution.java create mode 100644 src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/readme.md create mode 100644 src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/Solution.java create mode 100644 src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/readme.md create mode 100644 src/test/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3033_modify_the_matrix/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3035_maximum_palindromes_after_operations/SolutionTest.java create mode 100644 src/test/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/SolutionTest.java diff --git a/src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/Solution.java b/src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/Solution.java new file mode 100644 index 000000000..8fcd79b14 --- /dev/null +++ b/src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/Solution.java @@ -0,0 +1,33 @@ +package g3001_3100.s3031_minimum_time_to_revert_word_to_initial_state_ii; + +// #Hard #String #Hash_Function #String_Matching #Rolling_Hash +// #2024_03_01_Time_24_ms_(74.98%)_Space_55.1_MB_(14.85%) + +public class Solution { + public int minimumTimeToInitialState(String w, int q) { + char[] c = w.toCharArray(); + int[] lps = new int[c.length]; + int k; + for (int i = 1; i < lps.length; i++) { + if (c[i] == c[0]) { + lps[i] = 1; + } + k = lps[i - 1]; + while (k > 0) { + if (c[k] == c[i]) { + lps[i] = k + 1; + break; + } + k = lps[k - 1]; + } + } + k = lps[lps.length - 1]; + while (k > 0) { + if ((c.length - k) % q == 0) { + return (c.length - k) / q; + } + k = lps[k - 1]; + } + return (c.length + q - 1) / q; + } +} diff --git a/src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/readme.md b/src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/readme.md new file mode 100644 index 000000000..23b25239d --- /dev/null +++ b/src/main/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/readme.md @@ -0,0 +1,50 @@ +3031\. Minimum Time to Revert Word to Initial State II + +Hard + +You are given a **0-indexed** string `word` and an integer `k`. + +At every second, you must perform the following operations: + +* Remove the first `k` characters of `word`. +* Add any `k` characters to the end of `word`. + +**Note** that you do not necessarily need to add the same characters that you removed. However, you must perform **both** operations at every second. + +Return _the **minimum** time greater than zero required for_ `word` _to revert to its **initial** state_. + +**Example 1:** + +**Input:** word = "abacaba", k = 3 + +**Output:** 2 + +**Explanation:** At the 1st second, we remove characters "aba" from the prefix of word, and add characters "bac" to the end of word. Thus, word becomes equal to "cababac". At the 2nd second, we remove characters "cab" from the prefix of word, and add "aba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state. + +It can be shown that 2 seconds is the minimum time greater than zero required for word to revert to its initial state. + +**Example 2:** + +**Input:** word = "abacaba", k = 4 + +**Output:** 1 + +**Explanation:** At the 1st second, we remove characters "abac" from the prefix of word, and add characters "caba" to the end of word. Thus, word becomes equal to "abacaba" and reverts to its initial state. + +It can be shown that 1 second is the minimum time greater than zero required for word to revert to its initial state. + +**Example 3:** + +**Input:** word = "abcbabcd", k = 2 + +**Output:** 4 + +**Explanation:** At every second, we will remove the first 2 characters of word, and add the same characters to the end of word. After 4 seconds, word becomes equal to "abcbabcd" and reverts to its initial state. + +It can be shown that 4 seconds is the minimum time greater than zero required for word to revert to its initial state. + +**Constraints:** + +* 1 <= word.length <= 106 +* `1 <= k <= word.length` +* `word` consists only of lowercase English letters. diff --git a/src/main/java/g3001_3100/s3033_modify_the_matrix/Solution.java b/src/main/java/g3001_3100/s3033_modify_the_matrix/Solution.java new file mode 100644 index 000000000..a6ab0f6c8 --- /dev/null +++ b/src/main/java/g3001_3100/s3033_modify_the_matrix/Solution.java @@ -0,0 +1,22 @@ +package g3001_3100.s3033_modify_the_matrix; + +// #Easy #Array #Matrix #2024_03_01_Time_1_ms_(100.00%)_Space_45.4_MB_(77.37%) + +public class Solution { + public int[][] modifiedMatrix(int[][] matrix) { + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (matrix[i][j] == -1) { + int y = 0; + for (int[] ints : matrix) { + if (ints[j] > y) { + y = ints[j]; + } + } + matrix[i][j] = y; + } + } + } + return matrix; + } +} diff --git a/src/main/java/g3001_3100/s3033_modify_the_matrix/readme.md b/src/main/java/g3001_3100/s3033_modify_the_matrix/readme.md new file mode 100644 index 000000000..7b40e52fd --- /dev/null +++ b/src/main/java/g3001_3100/s3033_modify_the_matrix/readme.md @@ -0,0 +1,37 @@ +3033\. Modify the Matrix + +Easy + +Given a **0-indexed** `m x n` integer matrix `matrix`, create a new **0-indexed** matrix called `answer`. Make `answer` equal to `matrix`, then replace each element with the value `-1` with the **maximum** element in its respective column. + +Return _the matrix_ `answer`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2023/12/24/matrix1.png) + +**Input:** matrix = [[1,2,-1],[4,-1,6],[7,8,9]] + +**Output:** [[1,2,9],[4,8,6],[7,8,9]] + +**Explanation:** The diagram above shows the elements that are changed (in blue). +- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8. +- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2023/12/24/matrix2.png) + +**Input:** matrix = [[3,-1],[5,2]] + +**Output:** [[3,2],[5,2]] + +**Explanation:** The diagram above shows the elements that are changed (in blue). + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[i].length` +* `2 <= m, n <= 50` +* `-1 <= matrix[i][j] <= 100` +* The input is generated such that each column contains at least one non-negative integer. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/Solution.java b/src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/Solution.java new file mode 100644 index 000000000..a26ad85e5 --- /dev/null +++ b/src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/Solution.java @@ -0,0 +1,30 @@ +package g3001_3100.s3034_number_of_subarrays_that_match_a_pattern_i; + +// #Medium #Array #Hash_Function #String_Matching #Rolling_Hash +// #2024_03_01_Time_1_ms_(100.00%)_Space_43.9_MB_(97.20%) + +public class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + int n = nums.length; + int m = pattern.length; + int count = 0; + for (int i = 0; i <= n - m - 1; i++) { + int k = 0; + while (k < m) { + if (nums[i + k + 1] > nums[i + k] && pattern[k] == 1) { + k++; + } else if (nums[i + k + 1] == nums[i + k] && pattern[k] == 0) { + k++; + } else if (nums[i + k + 1] < nums[i + k] && pattern[k] == -1) { + k++; + } else { + break; + } + } + if (k == m) { + count++; + } + } + return count; + } +} diff --git a/src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/readme.md b/src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/readme.md new file mode 100644 index 000000000..7b40e52fd --- /dev/null +++ b/src/main/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/readme.md @@ -0,0 +1,37 @@ +3033\. Modify the Matrix + +Easy + +Given a **0-indexed** `m x n` integer matrix `matrix`, create a new **0-indexed** matrix called `answer`. Make `answer` equal to `matrix`, then replace each element with the value `-1` with the **maximum** element in its respective column. + +Return _the matrix_ `answer`. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2023/12/24/matrix1.png) + +**Input:** matrix = [[1,2,-1],[4,-1,6],[7,8,9]] + +**Output:** [[1,2,9],[4,8,6],[7,8,9]] + +**Explanation:** The diagram above shows the elements that are changed (in blue). +- We replace the value in the cell [1][1] with the maximum value in the column 1, that is 8. +- We replace the value in the cell [0][2] with the maximum value in the column 2, that is 9. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2023/12/24/matrix2.png) + +**Input:** matrix = [[3,-1],[5,2]] + +**Output:** [[3,2],[5,2]] + +**Explanation:** The diagram above shows the elements that are changed (in blue). + +**Constraints:** + +* `m == matrix.length` +* `n == matrix[i].length` +* `2 <= m, n <= 50` +* `-1 <= matrix[i][j] <= 100` +* The input is generated such that each column contains at least one non-negative integer. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/Solution.java b/src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/Solution.java new file mode 100644 index 000000000..0211d5e7b --- /dev/null +++ b/src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/Solution.java @@ -0,0 +1,51 @@ +package g3001_3100.s3035_maximum_palindromes_after_operations; + +// #Medium #Array #String #Hash_Table #Sorting #Greedy #Counting +// #2024_03_01_Time_4_ms_(99.13%)_Space_44.9_MB_(90.28%) + +public class Solution { + public int maxPalindromesAfterOperations(String[] words) { + int[] ar = new int[26]; + int[] dp = new int[101]; + int s = 0; + int p = 0; + int ans = 0; + for (String str : words) { + for (char c : str.toCharArray()) { + ar[c - 'a']++; + } + dp[str.length()]++; + } + for (int j : ar) { + s += j % 2; + p += (j / 2); + } + for (int i = 1; i < dp.length; i++) { + if (dp[i] > 0) { + if (i % 2 == 0) { + while (dp[i] > 0 && p > 0) { + p -= i / 2; + if (p >= 0) { + ans++; + } + dp[i]--; + } + } else { + while (dp[i] > 0 && (i == 1 || p > 0)) { + if (s == 0) { + s += 2; + p--; + } + s--; + p -= (i - 1) / 2; + if (p >= 0) { + ans++; + } + dp[i]--; + } + } + } + } + return ans; + } +} diff --git a/src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/readme.md b/src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/readme.md new file mode 100644 index 000000000..0fb8e076f --- /dev/null +++ b/src/main/java/g3001_3100/s3035_maximum_palindromes_after_operations/readme.md @@ -0,0 +1,53 @@ +3035\. Maximum Palindromes After Operations + +Medium + +You are given a **0-indexed** string array `words` having length `n` and containing **0-indexed** strings. + +You are allowed to perform the following operation **any** number of times (**including** **zero**): + +* Choose integers `i`, `j`, `x`, and `y` such that `0 <= i, j < n`, `0 <= x < words[i].length`, `0 <= y < words[j].length`, and **swap** the characters `words[i][x]` and `words[j][y]`. + +Return _an integer denoting the **maximum** number of palindromes_ `words` _can contain, after performing some operations._ + +**Note:** `i` and `j` may be equal during an operation. + +**Example 1:** + +**Input:** words = ["abbb","ba","aa"] + +**Output:** 3 + +**Explanation:** In this example, one way to get the maximum number of palindromes is: + +Choose i = 0, j = 1, x = 0, y = 0, so we swap words[0][0] and words[1][0]. words becomes ["bbbb","aa","aa"]. + +All strings in words are now palindromes. Hence, the maximum number of palindromes achievable is 3. + +**Example 2:** + +**Input:** words = ["abc","ab"] + +**Output:** 2 + +**Explanation:** In this example, one way to get the maximum number of palindromes is: + +Choose i = 0, j = 1, x = 1, y = 0, so we swap words[0][1] and words[1][0]. words becomes ["aac","bb"]. + +Choose i = 0, j = 0, x = 1, y = 2, so we swap words[0][1] and words[0][2]. words becomes ["aca","bb"]. + +Both strings are now palindromes. Hence, the maximum number of palindromes achievable is 2. + +**Example 3:** + +**Input:** words = ["cd","ef","a"] + +**Output:** 1 + +**Explanation:** In this example, there is no need to perform any operation. There is one palindrome in words "a". It can be shown that it is not possible to get more than one palindrome after any number of operations. Hence, the answer is 1. + +**Constraints:** + +* `1 <= words.length <= 1000` +* `1 <= words[i].length <= 100` +* `words[i]` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/Solution.java b/src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/Solution.java new file mode 100644 index 000000000..df24f8d37 --- /dev/null +++ b/src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/Solution.java @@ -0,0 +1,38 @@ +package g3001_3100.s3036_number_of_subarrays_that_match_a_pattern_ii; + +// #Hard #Array #Hash_Function #String_Matching #Rolling_Hash +// #2024_03_01_Time_5_ms_(98.27%)_Space_172.1_MB_(75.77%) + +public class Solution { + public int countMatchingSubarrays(int[] nums, int[] pattern) { + int n = nums.length; + int m = pattern.length; + int[] arr = new int[n - 1]; + for (int i = 0; i < n - 1; i++) { + if (nums[i + 1] > nums[i]) { + arr[i] = 1; + } else if (nums[i + 1] < nums[i]) { + arr[i] = -1; + } + } + int hash = 0; + int pHash = 0; + int base = 1; + for (int i = 0; i < m; i++) { + hash = hash * 3 + arr[i] + 1; + pHash = pHash * 3 + pattern[i] + 1; + base *= 3; + } + int count = 0; + for (int i = 0; i <= n - 1 - m; i++) { + if (hash == pHash) { + count++; + } + + if (i < n - 1 - m) { + hash = hash * 3 - base * (arr[i] + 1) + arr[i + m] + 1; + } + } + return count; + } +} diff --git a/src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/readme.md b/src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/readme.md new file mode 100644 index 000000000..f19200958 --- /dev/null +++ b/src/main/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/readme.md @@ -0,0 +1,36 @@ +3036\. Number of Subarrays That Match a Pattern II + +Hard + +You are given a **0-indexed** integer array `nums` of size `n`, and a **0-indexed** integer array `pattern` of size `m` consisting of integers `-1`, `0`, and `1`. + +A subarray `nums[i..j]` of size `m + 1` is said to match the `pattern` if the following conditions hold for each element `pattern[k]`: + +* `nums[i + k + 1] > nums[i + k]` if `pattern[k] == 1`. +* `nums[i + k + 1] == nums[i + k]` if `pattern[k] == 0`. +* `nums[i + k + 1] < nums[i + k]` if `pattern[k] == -1`. + +Return _the **count** of subarrays in_ `nums` _that match the_ `pattern`. + +**Example 1:** + +**Input:** nums = [1,2,3,4,5,6], pattern = [1,1] + +**Output:** 4 + +**Explanation:** The pattern [1,1] indicates that we are looking for strictly increasing subarrays of size 3. In the array nums, the subarrays [1,2,3], [2,3,4], [3,4,5], and [4,5,6] match this pattern. Hence, there are 4 subarrays in nums that match the pattern. + +**Example 2:** + +**Input:** nums = [1,4,4,1,3,5,5,3], pattern = [1,0,-1] + +**Output:** 2 + +**Explanation:** Here, the pattern [1,0,-1] indicates that we are looking for a sequence where the first number is smaller than the second, the second is equal to the third, and the third is greater than the fourth. In the array nums, the subarrays [1,4,4,1], and [3,5,5,3] match this pattern. Hence, there are 2 subarrays in nums that match the pattern. + +**Constraints:** + +* 2 <= n == nums.length <= 106 +* 1 <= nums[i] <= 109 +* `1 <= m == pattern.length < n` +* `-1 <= pattern[i] <= 1` \ No newline at end of file diff --git a/src/test/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/SolutionTest.java b/src/test/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/SolutionTest.java new file mode 100644 index 000000000..7996621c1 --- /dev/null +++ b/src/test/java/g3001_3100/s3031_minimum_time_to_revert_word_to_initial_state_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3031_minimum_time_to_revert_word_to_initial_state_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumTimeToInitialState() { + assertThat(new Solution().minimumTimeToInitialState("abacaba", 3), equalTo(2)); + } + + @Test + void minimumTimeToInitialState2() { + assertThat(new Solution().minimumTimeToInitialState("abacaba", 4), equalTo(1)); + } + + @Test + void minimumTimeToInitialState3() { + assertThat(new Solution().minimumTimeToInitialState("abcbabcd", 2), equalTo(4)); + } +} diff --git a/src/test/java/g3001_3100/s3033_modify_the_matrix/SolutionTest.java b/src/test/java/g3001_3100/s3033_modify_the_matrix/SolutionTest.java new file mode 100644 index 000000000..ec6c01664 --- /dev/null +++ b/src/test/java/g3001_3100/s3033_modify_the_matrix/SolutionTest.java @@ -0,0 +1,29 @@ +package g3001_3100.s3033_modify_the_matrix; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import com_github_leetcode.CommonUtils; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void modifiedMatrix() { + assertThat( + new Solution() + .modifiedMatrix( + CommonUtils + .convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,-1],[4,-1,6],[7,8,9]")), + equalTo( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,2,9],[4,8,6],[7,8,9]"))); + } + + @Test + void modifiedMatrix2() { + assertThat( + new Solution().modifiedMatrix(new int[][] {{3, -1}, {5, 2}}), + equalTo(new int[][] {{3, 2}, {5, 2}})); + } +} diff --git a/src/test/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/SolutionTest.java b/src/test/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/SolutionTest.java new file mode 100644 index 000000000..d79a02012 --- /dev/null +++ b/src/test/java/g3001_3100/s3034_number_of_subarrays_that_match_a_pattern_i/SolutionTest.java @@ -0,0 +1,25 @@ +package g3001_3100.s3034_number_of_subarrays_that_match_a_pattern_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countMatchingSubarrays() { + assertThat( + new Solution() + .countMatchingSubarrays(new int[] {1, 2, 3, 4, 5, 6}, new int[] {1, 1}), + equalTo(4)); + } + + @Test + void countMatchingSubarrays2() { + assertThat( + new Solution() + .countMatchingSubarrays( + new int[] {1, 4, 4, 1, 3, 5, 5, 3}, new int[] {1, 0, -1}), + equalTo(2)); + } +} diff --git a/src/test/java/g3001_3100/s3035_maximum_palindromes_after_operations/SolutionTest.java b/src/test/java/g3001_3100/s3035_maximum_palindromes_after_operations/SolutionTest.java new file mode 100644 index 000000000..e3e3a9d5a --- /dev/null +++ b/src/test/java/g3001_3100/s3035_maximum_palindromes_after_operations/SolutionTest.java @@ -0,0 +1,29 @@ +package g3001_3100.s3035_maximum_palindromes_after_operations; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxPalindromesAfterOperations() { + assertThat( + new Solution().maxPalindromesAfterOperations(new String[] {"abbb", "ba", "aa"}), + equalTo(3)); + } + + @Test + void maxPalindromesAfterOperations2() { + assertThat( + new Solution().maxPalindromesAfterOperations(new String[] {"abc", "ab"}), + equalTo(2)); + } + + @Test + void maxPalindromesAfterOperations3() { + assertThat( + new Solution().maxPalindromesAfterOperations(new String[] {"cd", "ef", "a"}), + equalTo(1)); + } +} diff --git a/src/test/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/SolutionTest.java b/src/test/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/SolutionTest.java new file mode 100644 index 000000000..a3d527921 --- /dev/null +++ b/src/test/java/g3001_3100/s3036_number_of_subarrays_that_match_a_pattern_ii/SolutionTest.java @@ -0,0 +1,25 @@ +package g3001_3100.s3036_number_of_subarrays_that_match_a_pattern_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countMatchingSubarrays() { + assertThat( + new Solution() + .countMatchingSubarrays(new int[] {1, 2, 3, 4, 5, 6}, new int[] {1, 1}), + equalTo(4)); + } + + @Test + void countMatchingSubarrays2() { + assertThat( + new Solution() + .countMatchingSubarrays( + new int[] {1, 4, 4, 1, 3, 5, 5, 3}, new int[] {1, 0, -1}), + equalTo(2)); + } +}