diff --git a/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.kt b/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.kt new file mode 100644 index 000000000..d2c77b3b3 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/Solution.kt @@ -0,0 +1,19 @@ +package g3301_3400.s3392_count_subarrays_of_length_three_with_a_condition + +// #Easy #2024_12_22_Time_3_ms_(100.00%)_Space_45_MB_(100.00%) + +class Solution { + fun countSubarrays(nums: IntArray): Int { + val window = 3 + var cnt = 0 + for (i in 0..nums.size - window) { + val first = nums[i].toFloat() + val second = nums[i + 1].toFloat() + val third = nums[i + 2].toFloat() + if (second / 2 == first + third) { + cnt++ + } + } + return cnt + } +} diff --git a/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/readme.md b/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/readme.md new file mode 100644 index 000000000..b502289ee --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/readme.md @@ -0,0 +1,32 @@ +3392\. Count Subarrays of Length Three With a Condition + +Easy + +Given an integer array `nums`, return the number of subarrays of length 3 such that the sum of the first and third numbers equals _exactly_ half of the second number. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [1,2,1,4,1] + +**Output:** 1 + +**Explanation:** + +Only the subarray `[1,4,1]` contains exactly 3 elements where the sum of the first and third numbers equals half the middle number. + +**Example 2:** + +**Input:** nums = [1,1,1] + +**Output:** 0 + +**Explanation:** + +`[1,1,1]` is the only subarray of length 3. However, its first and third numbers do not add to half the middle number. + +**Constraints:** + +* `3 <= nums.length <= 100` +* `-100 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.kt b/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.kt new file mode 100644 index 000000000..b908c0e62 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/Solution.kt @@ -0,0 +1,38 @@ +package g3301_3400.s3393_count_paths_with_the_given_xor_value + +// #Medium #2024_12_30_Time_57_(68.42%)_Space_73.12_(52.63%) + +class Solution { + private var m = -1 + private var n = -1 + private lateinit var dp: Array> + + fun countPathsWithXorValue(grid: Array, k: Int): Int { + m = grid.size + n = grid[0].size + dp = Array(m) { Array(n) { IntArray(16) { -1 } } } + return dfs(grid, 0, k, 0, 0) + } + + private fun dfs(grid: Array, xorVal: Int, k: Int, i: Int, j: Int): Int { + var xorVal = xorVal + if (i < 0 || j < 0 || j >= n || i >= m) { + return 0 + } + xorVal = xorVal xor grid[i][j] + if (dp[i][j][xorVal] != -1) { + return dp[i][j][xorVal] + } + if (i == m - 1 && j == n - 1 && xorVal == k) { + return 1 + } + val down = dfs(grid, xorVal, k, i + 1, j) + val right = dfs(grid, xorVal, k, i, j + 1) + dp[i][j][xorVal] = (down + right) % MOD + return dp[i][j][xorVal] + } + + companion object { + private val MOD = (1e9 + 7).toInt() + } +} diff --git a/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/readme.md b/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/readme.md new file mode 100644 index 000000000..121a32ba4 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/readme.md @@ -0,0 +1,57 @@ +3393\. Count Paths With the Given XOR Value + +Medium + +You are given a 2D integer array `grid` with size `m x n`. You are also given an integer `k`. + +Your task is to calculate the number of paths you can take from the top-left cell `(0, 0)` to the bottom-right cell `(m - 1, n - 1)` satisfying the following **constraints**: + +* You can either move to the right or down. Formally, from the cell `(i, j)` you may move to the cell `(i, j + 1)` or to the cell `(i + 1, j)` if the target cell _exists_. +* The `XOR` of all the numbers on the path must be **equal** to `k`. + +Return the total number of such paths. + +Since the answer can be very large, return the result **modulo** 109 + 7. + +**Example 1:** + +**Input:** grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11 + +**Output:** 3 + +**Explanation:** + +The 3 paths are: + +* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)` +* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)` +* `(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)` + +**Example 2:** + +**Input:** grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2 + +**Output:** 5 + +**Explanation:** + +The 5 paths are: + +* `(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)` +* `(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)` +* `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)` +* `(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)` +* `(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)` + +**Example 3:** + +**Input:** grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10 + +**Output:** 0 + +**Constraints:** + +* `1 <= m == grid.length <= 300` +* `1 <= n == grid[r].length <= 300` +* `0 <= grid[r][c] < 16` +* `0 <= k < 16` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.kt b/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.kt new file mode 100644 index 000000000..cd5245c6d --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/Solution.kt @@ -0,0 +1,55 @@ +package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections + +// #Medium #2024_12_22_Time_298_ms_(100.00%)_Space_132.4_MB_(100.00%) + +import kotlin.math.max + +@Suppress("unused") +class Solution { + fun checkValidCuts(n: Int, rectangles: Array): Boolean { + val m = rectangles.size + val xAxis = Array(m) { IntArray(2) } + val yAxis = Array(m) { IntArray(2) } + var ind = 0 + for (axis in rectangles) { + val startX = axis[0] + val startY = axis[1] + val endX = axis[2] + val endY = axis[3] + xAxis[ind] = intArrayOf(startX, endX) + yAxis[ind] = intArrayOf(startY, endY) + ind++ + } + + xAxis.sortWith( + Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] }, + ) + + yAxis.sortWith( + Comparator { a: IntArray, b: IntArray -> if (a[0] == b[0]) a[1] - b[1] else a[0] - b[0] }, + ) + val verticalCuts = findSections(xAxis) + if (verticalCuts > 2) { + return true + } + val horizontalCuts = findSections(yAxis) + return horizontalCuts > 2 + } + + private fun findSections(axis: Array): Int { + var end = axis[0][1] + var sections = 1 + for (i in 1.. axis[i][0]) { + end = max(end, axis[i][1]) + } else { + sections++ + end = axis[i][1] + } + if (sections > 2) { + return sections + } + } + return sections + } +} diff --git a/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/readme.md b/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/readme.md new file mode 100644 index 000000000..3c707e831 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/readme.md @@ -0,0 +1,57 @@ +3394\. Check if Grid can be Cut into Sections + +Medium + +You are given an integer `n` representing the dimensions of an `n x n` grid, with the origin at the bottom-left corner of the grid. You are also given a 2D array of coordinates `rectangles`, where `rectangles[i]` is in the form [startx, starty, endx, endy], representing a rectangle on the grid. Each rectangle is defined as follows: + +* (startx, starty): The bottom-left corner of the rectangle. +* (endx, endy): The top-right corner of the rectangle. + +**Note** that the rectangles do not overlap. Your task is to determine if it is possible to make **either two horizontal or two vertical cuts** on the grid such that: + +* Each of the three resulting sections formed by the cuts contains **at least** one rectangle. +* Every rectangle belongs to **exactly** one section. + +Return `true` if such cuts can be made; otherwise, return `false`. + +**Example 1:** + +**Input:** n = 5, rectangles = [[1,0,5,2],[0,2,2,4],[3,2,5,3],[0,4,4,5]] + +**Output:** true + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/23/tt1drawio.png) + +The grid is shown in the diagram. We can make horizontal cuts at `y = 2` and `y = 4`. Hence, output is true. + +**Example 2:** + +**Input:** n = 4, rectangles = [[0,0,1,1],[2,0,3,4],[0,2,2,3],[3,0,4,3]] + +**Output:** true + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/23/tc2drawio.png) + +We can make vertical cuts at `x = 2` and `x = 3`. Hence, output is true. + +**Example 3:** + +**Input:** n = 4, rectangles = [[0,2,2,4],[1,0,3,2],[2,2,3,4],[3,0,4,2],[3,2,4,4]] + +**Output:** false + +**Explanation:** + +We cannot make two horizontal or two vertical cuts that satisfy the conditions. Hence, output is false. + +**Constraints:** + +* 3 <= n <= 109 +* 3 <= rectangles.length <= 105 +* `0 <= rectangles[i][0] < rectangles[i][2] <= n` +* `0 <= rectangles[i][1] < rectangles[i][3] <= n` +* No two rectangles overlap. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.kt b/src/main/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.kt new file mode 100644 index 000000000..4f21cdd3f --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/Solution.kt @@ -0,0 +1,190 @@ +package g3301_3400.s3395_subsequences_with_a_unique_middle_mode_i + +// #Hard #2024_12_22_Time_485_ms_(100.00%)_Space_50.6_MB_(100.00%) + +class Solution { + fun subsequencesWithMiddleMode(a: IntArray): Int { + val n = a.size + // Create a dictionary to store indices of each number + val dict: MutableMap> = HashMap() + for (i in 0.. ArrayList() }.add(i) + } + var ans = 0L + // Iterate over each unique number and its indices + for (entry in dict.entries) { + val b: MutableList = entry.value + val m = b.size + for (k in 0.. = midEntry.value + val m = b.size + for (tmpEntry in dict.entries) { + if (midEntry.key != tmpEntry.key) { + val c: MutableList = tmpEntry.value + val size = c.size + var k = 0 + var j = 0 + while (k < m) { + val i: Int = b[k] + val r = m - 1 - k + val u = i - k + val v = (n - 1 - i) - r + while (j < size && c[j] < i) { + j++ + } + val x = j + val y = size - x + dif = + ( + ( + dif + + convert(k, 1) * + convert(x, 1) % + MOD + * convert(y, 1) % + MOD + * convert(v - y, 1) % + MOD + ) % + MOD + ) + dif = + ( + ( + dif + + convert(k, 1) * + convert(y, 2) % + MOD + * convert(u - x, 1) % + MOD + ) % + MOD + ) + dif = + ( + ( + dif + convert(k, 1) * convert(x, 1) % MOD * convert( + y, + 2, + ) % MOD + ) % + MOD + ) + + dif = + ( + ( + dif + + convert(r, 1) * + convert(x, 1) % + MOD + * convert(y, 1) % + MOD + * convert(u - x, 1) % + MOD + ) % + MOD + ) + dif = + ( + ( + dif + + convert(r, 1) * + convert(x, 2) % + MOD + * convert(v - y, 1) % + MOD + ) % + MOD + ) + dif = + ( + ( + dif + convert(r, 1) * convert(x, 2) % MOD * convert( + y, + 1, + ) % MOD + ) % + MOD + ) + k++ + } + } + } + } + return ((ans - dif + MOD) % MOD).toInt() + } + + private fun convert(n: Int, k: Int): Long { + if (k > n) { + return 0 + } + if (k == 0 || k == n) { + return 1 + } + var res: Long = 1 + for (i in 0..109 + 7. + +A **mode** of a sequence of numbers is defined as the element that appears the **maximum** number of times in the sequence. + +A sequence of numbers contains a **unique mode** if it has only one mode. + +A sequence of numbers `seq` of size 5 contains a **unique middle mode** if the _middle element_ (`seq[2]`) is a **unique mode**. + +A **subsequence** is a **non-empty** array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. + +**Example 1:** + +**Input:** nums = [1,1,1,1,1,1] + +**Output:** 6 + +**Explanation:** + +`[1, 1, 1, 1, 1]` is the only subsequence of size 5 that can be formed, and it has a unique middle mode of 1. This subsequence can be formed in 6 different ways, so the output is 6. + +**Example 2:** + +**Input:** nums = [1,2,2,3,3,4] + +**Output:** 4 + +**Explanation:** + +`[1, 2, 2, 3, 4]` and `[1, 2, 3, 3, 4]` each have a unique middle mode because the number at index 2 has the greatest frequency in the subsequence. `[1, 2, 2, 3, 3]` does not have a unique middle mode because 2 and 3 appear twice. + +**Example 3:** + +**Input:** nums = [0,1,2,3,4,5,6,7,8] + +**Output:** 0 + +**Explanation:** + +There is no subsequence of length 5 with a unique middle mode. + +**Constraints:** + +* `5 <= nums.length <= 1000` +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/Solution.kt b/src/main/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/Solution.kt new file mode 100644 index 000000000..01721c5d2 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/Solution.kt @@ -0,0 +1,34 @@ +package g3301_3400.s3396_minimum_number_of_operations_to_make_elements_in_array_distinct + +// #Easy #2024_12_22_Time_9_ms_(100.00%)_Space_37.9_MB_(100.00%) + +import kotlin.math.min + +class Solution { + fun minimumOperations(nums: IntArray): Int { + val map: MutableMap = HashMap() + var dupct = 0 + for (num in nums) { + map.put(num, map.getOrDefault(num, 0) + 1) + if (map[num] == 2) { + dupct++ + } + } + val n = nums.size + var i = 0 + var op = 0 + while (dupct > 0) { + op++ + val limit = min(n, (i + 3)) + while (i < limit) { + val `val`: Int = map[nums[i]]!! + if (`val` == 2) { + dupct-- + } + map.put(nums[i], `val` - 1) + i++ + } + } + return op + } +} diff --git a/src/main/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/readme.md b/src/main/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/readme.md new file mode 100644 index 000000000..ae8e9e0a8 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/readme.md @@ -0,0 +1,50 @@ +3396\. Minimum Number of Operations to Make Elements in Array Distinct + +Easy + +You are given an integer array `nums`. You need to ensure that the elements in the array are **distinct**. To achieve this, you can perform the following operation any number of times: + +* Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements. + +**Note** that an empty array is considered to have distinct elements. Return the **minimum** number of operations needed to make the elements in the array distinct. + +**Example 1:** + +**Input:** nums = [1,2,3,4,2,3,3,5,7] + +**Output:** 2 + +**Explanation:** + +* In the first operation, the first 3 elements are removed, resulting in the array `[4, 2, 3, 3, 5, 7]`. +* In the second operation, the next 3 elements are removed, resulting in the array `[3, 5, 7]`, which has distinct elements. + +Therefore, the answer is 2. + +**Example 2:** + +**Input:** nums = [4,5,6,4,4] + +**Output:** 2 + +**Explanation:** + +* In the first operation, the first 3 elements are removed, resulting in the array `[4, 4]`. +* In the second operation, all remaining elements are removed, resulting in an empty array. + +Therefore, the answer is 2. + +**Example 3:** + +**Input:** nums = [6,7,8,9] + +**Output:** 0 + +**Explanation:** + +The array already contains distinct elements. Therefore, the answer is 0. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/Solution.kt b/src/main/kotlin/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/Solution.kt new file mode 100644 index 000000000..a84a0ba55 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/Solution.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3397_maximum_number_of_distinct_elements_after_operations + +// #Medium #2024_12_22_Time_517_ms_(100.00%)_Space_61.2_MB_(100.00%) + +import kotlin.math.max + +class Solution { + fun maxDistinctElements(nums: IntArray, k: Int): Int { + nums.sort() + var next = nums[0] - k + 1 + val n = nums.size + var ans = 1 + for (i in 1..1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* 0 <= k <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/Solution.kt b/src/main/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/Solution.kt new file mode 100644 index 000000000..27c850d6c --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/Solution.kt @@ -0,0 +1,67 @@ +package g3301_3400.s3398_smallest_substring_with_identical_characters_i + +// #Hard #2024_12_24_Time_2_ms_(100.00%)_Space_36.4_MB_(92.86%) + +class Solution { + fun minLength(s: String, ops: Int): Int { + val arr2 = s.toCharArray() + var q = '0'.code + var w = '1'.code + var p1 = ops + var p2 = ops + for (i in 0..= 0 || p2 >= 0) { + return 1 + } + var low = 2 + var high = s.length + var ans = 0 + val n = s.length + while (low <= high) { + val mid = (low + high) / 2 + val arr = s.toCharArray() + var p = ops + var c = 1 + for (i in 1.. mid) { + if (arr[i - 1] == '0') { + arr[i - 1] = '1' + } else { + arr[i - 1] = '0' + } + p-- + c = 0 + } + } + if (p < 0) { + low = mid + 1 + } else { + ans = mid + high = mid - 1 + } + } + return ans + } +} diff --git a/src/main/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/readme.md b/src/main/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/readme.md new file mode 100644 index 000000000..eb273a6bd --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/readme.md @@ -0,0 +1,45 @@ +3398\. Smallest Substring With Identical Characters I + +Hard + +You are given a binary string `s` of length `n` and an integer `numOps`. + +You are allowed to perform the following operation on `s` **at most** `numOps` times: + +* Select any index `i` (where `0 <= i < n`) and **flip** `s[i]`. If `s[i] == '1'`, change `s[i]` to `'0'` and vice versa. + +You need to **minimize** the length of the **longest** substring of `s` such that all the characters in the substring are **identical**. + +Return the **minimum** length after the operations. + +**Example 1:** + +**Input:** s = "000001", numOps = 1 + +**Output:** 2 + +**Explanation:** + +By changing `s[2]` to `'1'`, `s` becomes `"001001"`. The longest substrings with identical characters are `s[0..1]` and `s[3..4]`. + +**Example 2:** + +**Input:** s = "0000", numOps = 2 + +**Output:** 1 + +**Explanation:** + +By changing `s[0]` and `s[2]` to `'1'`, `s` becomes `"1010"`. + +**Example 3:** + +**Input:** s = "0101", numOps = 0 + +**Output:** 1 + +**Constraints:** + +* `1 <= n == s.length <= 1000` +* `s` consists only of `'0'` and `'1'`. +* `0 <= numOps <= n` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.kt new file mode 100644 index 000000000..d96f624e7 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/Solution.kt @@ -0,0 +1,71 @@ +package g3301_3400.s3399_smallest_substring_with_identical_characters_ii + +// #Hard #2024_12_24_Time_26_ms_(100.00%)_Space_40.2_MB_(100.00%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun minLength(s: String, numOps: Int): Int { + val b = s.toByteArray() + var flips1 = 0 + var flips2 = 0 + for (i in b.indices) { + val e1 = (if (i % 2 == 0) '0' else '1').code.toByte() + val e2 = (if (i % 2 == 0) '1' else '0').code.toByte() + if (b[i] != e1) { + flips1++ + } + if (b[i] != e2) { + flips2++ + } + } + val flips = min(flips1, flips2) + if (flips <= numOps) { + return 1 + } + val seg: MutableList = ArrayList() + var count = 1 + var max = 1 + for (i in 1.., ops: Int): Boolean { + var ops = ops + for (i in seg) { + val x = i / (sz + 1) + ops -= x + if (ops < 0) { + return false + } + } + return true + } +} diff --git a/src/main/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/readme.md b/src/main/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/readme.md new file mode 100644 index 000000000..424c9c840 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/readme.md @@ -0,0 +1,45 @@ +3399\. Smallest Substring With Identical Characters II + +Hard + +You are given a binary string `s` of length `n` and an integer `numOps`. + +You are allowed to perform the following operation on `s` **at most** `numOps` times: + +* Select any index `i` (where `0 <= i < n`) and **flip** `s[i]`. If `s[i] == '1'`, change `s[i]` to `'0'` and vice versa. + +You need to **minimize** the length of the **longest** substring of `s` such that all the characters in the substring are **identical**. + +Return the **minimum** length after the operations. + +**Example 1:** + +**Input:** s = "000001", numOps = 1 + +**Output:** 2 + +**Explanation:** + +By changing `s[2]` to `'1'`, `s` becomes `"001001"`. The longest substrings with identical characters are `s[0..1]` and `s[3..4]`. + +**Example 2:** + +**Input:** s = "0000", numOps = 2 + +**Output:** 1 + +**Explanation:** + +By changing `s[0]` and `s[2]` to `'1'`, `s` becomes `"1010"`. + +**Example 3:** + +**Input:** s = "0101", numOps = 0 + +**Output:** 1 + +**Constraints:** + +* 1 <= n == s.length <= 105 +* `s` consists only of `'0'` and `'1'`. +* `0 <= numOps <= n` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/Solution.kt b/src/main/kotlin/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/Solution.kt new file mode 100644 index 000000000..f386ad3ba --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/Solution.kt @@ -0,0 +1,18 @@ +package g3401_3500.s3402_minimum_operations_to_make_columns_strictly_increasing + +// #Easy #2024_12_29_Time_1_(100.00%)_Space_44.94_(100.00%) + +class Solution { + fun minimumOperations(grid: Array): Int { + var ans = 0 + for (c in grid[0].indices) { + for (r in 1..0th column strictly increasing, we can apply 3 operations on `grid[1][0]`, 2 operations on `grid[2][0]`, and 6 operations on `grid[3][0]`. +* To make the 1st column strictly increasing, we can apply 4 operations on `grid[3][1]`. + +![](https://assets.leetcode.com/uploads/2024/11/10/firstexample.png) + +**Example 2:** + +**Input:** grid = [[3,2,1],[2,1,0],[1,2,3]] + +**Output:** 12 + +**Explanation:** + +* To make the 0th column strictly increasing, we can apply 2 operations on `grid[1][0]`, and 4 operations on `grid[2][0]`. +* To make the 1st column strictly increasing, we can apply 2 operations on `grid[1][1]`, and 2 operations on `grid[2][1]`. +* To make the 2nd column strictly increasing, we can apply 2 operations on `grid[1][2]`. + +![](https://assets.leetcode.com/uploads/2024/11/10/secondexample.png) + +**Constraints:** + +* `m == grid.length` +* `n == grid[i].length` +* `1 <= m, n <= 50` +* `0 <= grid[i][j] < 2500` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.kt b/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.kt new file mode 100644 index 000000000..1d38bf7ec --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/Solution.kt @@ -0,0 +1,27 @@ +package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i + +// #Medium #2024_12_29_Time_22_(100.00%)_Space_38.72_(100.00%) + +import kotlin.math.min + +class Solution { + fun answerString(word: String, numFriends: Int): String { + if (numFriends == 1) { + return word + } + val n = word.length + val maxlen = n - numFriends + 1 + var maxchar = word[0] + var res = "" + for (i in 0..= maxchar) { + val curr = word.substring(i, min((i + maxlen), n)) + if (curr > res) { + res = curr + } + maxchar = word[i] + } + } + return res + } +} diff --git a/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/readme.md b/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/readme.md new file mode 100644 index 000000000..75a49bd2f --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/readme.md @@ -0,0 +1,45 @@ +3403\. Find the Lexicographically Largest String From the Box I + +Medium + +You are given a string `word`, and an integer `numFriends`. + +Alice is organizing a game for her `numFriends` friends. There are multiple rounds in the game, where in each round: + +* `word` is split into `numFriends` **non-empty** strings, such that no previous round has had the **exact** same split. +* All the split words are put into a box. + +Find the **lexicographically largest** string from the box after all the rounds are finished. + +A string `a` is **lexicographically smaller** than a string `b` if in the first position where `a` and `b` differ, string `a` has a letter that appears earlier in the alphabet than the corresponding letter in `b`. + If the first `min(a.length, b.length)` characters do not differ, then the shorter string is the lexicographically smaller one. + +**Example 1:** + +**Input:** word = "dbca", numFriends = 2 + +**Output:** "dbc" + +**Explanation:** + +All possible splits are: + +* `"d"` and `"bca"`. +* `"db"` and `"ca"`. +* `"dbc"` and `"a"`. + +**Example 2:** + +**Input:** word = "gggg", numFriends = 4 + +**Output:** "g" + +**Explanation:** + +The only possible split is: `"g"`, `"g"`, `"g"`, and `"g"`. + +**Constraints:** + +* 1 <= word.length <= 5 * 103 +* `word` consists only of lowercase English letters. +* `1 <= numFriends <= word.length` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3404_count_special_subsequences/Solution.kt b/src/main/kotlin/g3401_3500/s3404_count_special_subsequences/Solution.kt new file mode 100644 index 000000000..b55ff4356 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3404_count_special_subsequences/Solution.kt @@ -0,0 +1,23 @@ +package g3401_3500.s3404_count_special_subsequences + +// #Medium #2024_12_29_Time_276_(100.00%)_Space_60.90_(100.00%) + +class Solution { + fun numberOfSubsequences(nums: IntArray): Long { + val freq: MutableMap = HashMap() + var ans: Long = 0 + for (r in 4.. 1`, `r - q > 1` and `s - r > 1`. + +A subsequence is a sequence derived from the array by deleting zero or more elements without changing the order of the remaining elements. + +Return the _number_ of different **special** **subsequences** in `nums`. + +**Example 1:** + +**Input:** nums = [1,2,3,4,3,6,1] + +**Output:** 1 + +**Explanation:** + +There is one special subsequence in `nums`. + +* `(p, q, r, s) = (0, 2, 4, 6)`: + * This corresponds to elements `(1, 3, 3, 1)`. + * `nums[p] * nums[r] = nums[0] * nums[4] = 1 * 3 = 3` + * `nums[q] * nums[s] = nums[2] * nums[6] = 3 * 1 = 3` + +**Example 2:** + +**Input:** nums = [3,4,3,4,3,4,3,4] + +**Output:** 3 + +**Explanation:** + +There are three special subsequences in `nums`. + +* `(p, q, r, s) = (0, 2, 4, 6)`: + * This corresponds to elements `(3, 3, 3, 3)`. + * `nums[p] * nums[r] = nums[0] * nums[4] = 3 * 3 = 9` + * `nums[q] * nums[s] = nums[2] * nums[6] = 3 * 3 = 9` +* `(p, q, r, s) = (1, 3, 5, 7)`: + * This corresponds to elements `(4, 4, 4, 4)`. + * `nums[p] * nums[r] = nums[1] * nums[5] = 4 * 4 = 16` + * `nums[q] * nums[s] = nums[3] * nums[7] = 4 * 4 = 16` +* `(p, q, r, s) = (0, 2, 5, 7)`: + * This corresponds to elements `(3, 3, 4, 4)`. + * `nums[p] * nums[r] = nums[0] * nums[5] = 3 * 4 = 12` + * `nums[q] * nums[s] = nums[2] * nums[7] = 3 * 4 = 12` + +**Constraints:** + +* `7 <= nums.length <= 1000` +* `1 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.kt new file mode 100644 index 000000000..53aa6c48f --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/Solution.kt @@ -0,0 +1,44 @@ +package g3401_3500.s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements + +// #Hard #2024_12_29_Time_57_(100.00%)_Space_37.63_(100.00%) + +class Solution { + fun countGoodArrays(n: Int, m: Int, k: Int): Int { + val f = LongArray(n + 1) + f[0] = 1 + f[1] = 1 + for (i in 2.. 0) { + if (e % 2 == 1L) { + ans = (ans * b) % MOD + } + b = (b * b) % MOD + e = e shr 1 + } + return ans + } + + private fun comb(n: Int, r: Int, f: LongArray): Long { + return f[n] * ff(f[r]) % MOD * ff(f[n - r]) % MOD + } + + private fun ff(x: Long): Long { + return ex(x, MOD - 2L) + } + + companion object { + private val MOD = (1e9 + 7).toInt() + } +} diff --git a/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/readme.md b/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/readme.md new file mode 100644 index 000000000..8aebabcb3 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/readme.md @@ -0,0 +1,50 @@ +3405\. Count the Number of Arrays with K Matching Adjacent Elements + +Hard + +You are given three integers `n`, `m`, `k`. A **good array** `arr` of size `n` is defined as follows: + +* Each element in `arr` is in the **inclusive** range `[1, m]`. +* _Exactly_ `k` indices `i` (where `1 <= i < n`) satisfy the condition `arr[i - 1] == arr[i]`. + +Return the number of **good arrays** that can be formed. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** n = 3, m = 2, k = 1 + +**Output:** 4 + +**Explanation:** + +* There are 4 good arrays. They are `[1, 1, 2]`, `[1, 2, 2]`, `[2, 1, 1]` and `[2, 2, 1]`. +* Hence, the answer is 4. + +**Example 2:** + +**Input:** n = 4, m = 2, k = 2 + +**Output:** 6 + +**Explanation:** + +* The good arrays are `[1, 1, 1, 2]`, `[1, 1, 2, 2]`, `[1, 2, 2, 2]`, `[2, 1, 1, 1]`, `[2, 2, 1, 1]` and `[2, 2, 2, 1]`. +* Hence, the answer is 6. + +**Example 3:** + +**Input:** n = 5, m = 2, k = 0 + +**Output:** 2 + +**Explanation:** + +* The good arrays are `[1, 2, 1, 2, 1]` and `[2, 1, 2, 1, 2]`. Hence, the answer is 2. + +**Constraints:** + +* 1 <= n <= 105 +* 1 <= m <= 105 +* `0 <= k <= n - 1` \ No newline at end of file diff --git a/src/test/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/SolutionTest.kt new file mode 100644 index 000000000..9771b6601 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3392_count_subarrays_of_length_three_with_a_condition/SolutionTest.kt @@ -0,0 +1,20 @@ +package g3301_3400.s3392_count_subarrays_of_length_three_with_a_condition + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countSubarrays() { + assertThat( + Solution().countSubarrays(intArrayOf(1, 2, 1, 4, 1)), + equalTo(1), + ) + } + + @Test + fun countSubarrays2() { + assertThat(Solution().countSubarrays(intArrayOf(1, 1, 1)), equalTo(0)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/SolutionTest.kt new file mode 100644 index 000000000..6a73e4474 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3393_count_paths_with_the_given_xor_value/SolutionTest.kt @@ -0,0 +1,43 @@ +package g3301_3400.s3393_count_paths_with_the_given_xor_value + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countPathsWithXorValue() { + assertThat( + Solution() + .countPathsWithXorValue( + arrayOf(intArrayOf(2, 1, 5), intArrayOf(7, 10, 0), intArrayOf(12, 6, 4)), + 11, + ), + equalTo(3), + ) + } + + @Test + fun countPathsWithXorValue2() { + assertThat( + Solution() + .countPathsWithXorValue( + arrayOf(intArrayOf(1, 3, 3, 3), intArrayOf(0, 3, 3, 2), intArrayOf(3, 0, 1, 1)), + 2, + ), + equalTo(5), + ) + } + + @Test + fun countPathsWithXorValue3() { + assertThat( + Solution() + .countPathsWithXorValue( + arrayOf(intArrayOf(1, 1, 1, 2), intArrayOf(3, 0, 3, 2), intArrayOf(3, 0, 2, 2)), + 10, + ), + equalTo(0), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/SolutionTest.kt new file mode 100644 index 000000000..3faa363ee --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3394_check_if_grid_can_be_cut_into_sections/SolutionTest.kt @@ -0,0 +1,59 @@ +package g3301_3400.s3394_check_if_grid_can_be_cut_into_sections + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun checkValidCuts() { + assertThat( + Solution() + .checkValidCuts( + 5, + arrayOf( + intArrayOf(1, 0, 5, 2), + intArrayOf(0, 2, 2, 4), + intArrayOf(3, 2, 5, 3), + intArrayOf(0, 4, 4, 5), + ), + ), + equalTo(true), + ) + } + + @Test + fun checkValidCuts2() { + assertThat( + Solution() + .checkValidCuts( + 4, + arrayOf( + intArrayOf(0, 0, 1, 1), + intArrayOf(2, 0, 3, 4), + intArrayOf(0, 2, 2, 3), + intArrayOf(3, 0, 4, 3), + ), + ), + equalTo(true), + ) + } + + @Test + fun checkValidCuts3() { + assertThat( + Solution() + .checkValidCuts( + 4, + arrayOf( + intArrayOf(0, 2, 2, 4), + intArrayOf(1, 0, 3, 2), + intArrayOf(2, 2, 3, 4), + intArrayOf(3, 0, 4, 2), + intArrayOf(3, 2, 4, 4), + ), + ), + equalTo(false), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/SolutionTest.kt new file mode 100644 index 000000000..6e47c35ab --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3395_subsequences_with_a_unique_middle_mode_i/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3301_3400.s3395_subsequences_with_a_unique_middle_mode_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun subsequencesWithMiddleMode() { + assertThat( + Solution().subsequencesWithMiddleMode(intArrayOf(1, 1, 1, 1, 1, 1)), + equalTo(6), + ) + } + + @Test + fun subsequencesWithMiddleMode2() { + assertThat( + Solution().subsequencesWithMiddleMode(intArrayOf(1, 2, 2, 3, 3, 4)), + equalTo(4), + ) + } + + @Test + fun subsequencesWithMiddleMode3() { + assertThat( + Solution().subsequencesWithMiddleMode(intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8)), + equalTo(0), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/SolutionTest.kt new file mode 100644 index 000000000..cd44d22ef --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3396_minimum_number_of_operations_to_make_elements_in_array_distinct/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3301_3400.s3396_minimum_number_of_operations_to_make_elements_in_array_distinct + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumOperations() { + assertThat( + Solution().minimumOperations(intArrayOf(1, 2, 3, 4, 2, 3, 3, 5, 7)), + equalTo(2), + ) + } + + @Test + fun minimumOperations2() { + assertThat( + Solution().minimumOperations(intArrayOf(4, 5, 6, 4, 4)), + equalTo(2), + ) + } + + @Test + fun minimumOperations3() { + assertThat( + Solution().minimumOperations(intArrayOf(6, 7, 8, 9)), + equalTo(0), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/SolutionTest.kt new file mode 100644 index 000000000..e41c7261e --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3397_maximum_number_of_distinct_elements_after_operations/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3397_maximum_number_of_distinct_elements_after_operations + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxDistinctElements() { + assertThat( + Solution().maxDistinctElements(intArrayOf(1, 2, 2, 3, 3, 4), 2), + equalTo(6), + ) + } + + @Test + fun maxDistinctElements2() { + assertThat( + Solution().maxDistinctElements(intArrayOf(4, 4, 4, 4), 1), + equalTo(3), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.kt new file mode 100644 index 000000000..611719917 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3398_smallest_substring_with_identical_characters_i/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3301_3400.s3398_smallest_substring_with_identical_characters_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minLength() { + assertThat(Solution().minLength("000001", 1), equalTo(2)) + } + + @Test + fun minLength2() { + assertThat(Solution().minLength("0000", 2), equalTo(1)) + } + + @Test + fun minLength3() { + assertThat(Solution().minLength("0101", 0), equalTo(1)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.kt new file mode 100644 index 000000000..8060a8e41 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3399_smallest_substring_with_identical_characters_ii/SolutionTest.kt @@ -0,0 +1,32 @@ +package g3301_3400.s3399_smallest_substring_with_identical_characters_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minLength() { + assertThat(Solution().minLength("000001", 1), equalTo(2)) + } + + @Test + fun minLength2() { + assertThat(Solution().minLength("0000", 2), equalTo(1)) + } + + @Test + fun minLength3() { + assertThat(Solution().minLength("0101", 0), equalTo(1)) + } + + @Test + fun minLength4() { + assertThat(Solution().minLength("000", 0), equalTo(3)) + } + + @Test + fun minLength5() { + assertThat(Solution().minLength("000001", 1), equalTo(2)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/SolutionTest.kt new file mode 100644 index 000000000..7ffed4979 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3402_minimum_operations_to_make_columns_strictly_increasing/SolutionTest.kt @@ -0,0 +1,36 @@ +package g3401_3500.s3402_minimum_operations_to_make_columns_strictly_increasing + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumOperations() { + assertThat( + Solution().minimumOperations( + arrayOf( + intArrayOf(3, 2), + intArrayOf(1, 3), + intArrayOf(3, 4), + intArrayOf(0, 1), + ), + ), + equalTo(15), + ) + } + + @Test + fun minimumOperations2() { + assertThat( + Solution().minimumOperations( + arrayOf( + intArrayOf(3, 2, 1), + intArrayOf(2, 1, 0), + intArrayOf(1, 2, 3), + ), + ), + equalTo(12), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.kt new file mode 100644 index 000000000..d3a7281d8 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3403_find_the_lexicographically_largest_string_from_the_box_i/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3401_3500.s3403_find_the_lexicographically_largest_string_from_the_box_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun answerString() { + assertThat(Solution().answerString("dbca", 2), equalTo("dbc")) + } + + @Test + fun answerString2() { + assertThat(Solution().answerString("gggg", 4), equalTo("g")) + } + + @Test + fun answerString3() { + assertThat(Solution().answerString("a", 1), equalTo("a")) + } +} diff --git a/src/test/kotlin/g3401_3500/s3404_count_special_subsequences/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3404_count_special_subsequences/SolutionTest.kt new file mode 100644 index 000000000..ad13d2084 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3404_count_special_subsequences/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3401_3500.s3404_count_special_subsequences + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun numberOfSubsequences() { + assertThat( + Solution().numberOfSubsequences(intArrayOf(1, 2, 3, 4, 3, 6, 1)), + equalTo(1L), + ) + } + + @Test + fun numberOfSubsequences2() { + assertThat( + Solution().numberOfSubsequences(intArrayOf(3, 4, 3, 4, 3, 4, 3, 4)), + equalTo(3L), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/SolutionTest.kt new file mode 100644 index 000000000..3b3f7829d --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3401_3500.s3405_count_the_number_of_arrays_with_k_matching_adjacent_elements + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countGoodArrays() { + assertThat(Solution().countGoodArrays(3, 2, 1), equalTo(4)) + } + + @Test + fun countGoodArrays2() { + assertThat(Solution().countGoodArrays(4, 2, 2), equalTo(6)) + } + + @Test + fun countGoodArrays3() { + assertThat(Solution().countGoodArrays(5, 2, 0), equalTo(2)) + } +}