diff --git a/src/main/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/Solution.java b/src/main/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/Solution.java new file mode 100644 index 000000000..288ca7534 --- /dev/null +++ b/src/main/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/Solution.java @@ -0,0 +1,21 @@ +package g3001_3100.s3082_find_the_sum_of_the_power_of_all_subsequences; + +// #Hard #Array #Dynamic_Programming #2024_04_17_Time_1_ms_(100.00%)_Space_42.5_MB_(98.13%) + +public class Solution { + public int sumOfPower(int[] nums, int k) { + final int kMod = 1_000_000_007; + int[] dp = new int[k + 1]; + dp[0] = 1; + for (final int num : nums) { + for (int i = k; i >= 0; --i) { + if (i < num) { + dp[i] = (int) ((dp[i] * 2L) % kMod); + } else { + dp[i] = (int) ((dp[i] * 2L + dp[i - num]) % kMod); + } + } + } + return dp[k]; + } +} diff --git a/src/main/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/readme.md b/src/main/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/readme.md new file mode 100644 index 000000000..c37a9a0b4 --- /dev/null +++ b/src/main/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/readme.md @@ -0,0 +1,59 @@ +3082\. Find the Sum of the Power of All Subsequences + +Hard + +You are given an integer array `nums` of length `n` and a **positive** integer `k`. + +The **power** of an array of integers is defined as the number of subsequences with their sum **equal** to `k`. + +Return _the **sum** of **power** of all subsequences of_ `nums`_._ + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** nums = [1,2,3], k = 3 + +**Output:** 6 + +**Explanation:** + +There are `5` subsequences of nums with non-zero power: + +* The subsequence [**1**,**2**,**3**] has `2` subsequences with `sum == 3`: [1,2,3] and [1,2,3]. +* The subsequence [**1**,2,**3**] has `1` subsequence with `sum == 3`: [1,2,3]. +* The subsequence [1,**2**,**3**] has `1` subsequence with `sum == 3`: [1,2,3]. +* The subsequence [**1**,**2**,3] has `1` subsequence with `sum == 3`: [1,2,3]. +* The subsequence [1,2,**3**] has `1` subsequence with `sum == 3`: [1,2,3]. + +Hence the answer is `2 + 1 + 1 + 1 + 1 = 6`. + +**Example 2:** + +**Input:** nums = [2,3,3], k = 5 + +**Output:** 4 + +**Explanation:** + +There are `3` subsequences of nums with non-zero power: + +* The subsequence [**2**,**3**,**3**] has 2 subsequences with `sum == 5`: [2,3,3] and [2,3,3]. +* The subsequence [**2**,3,**3**] has 1 subsequence with `sum == 5`: [2,3,3]. +* The subsequence [**2**,**3**,3] has 1 subsequence with `sum == 5`: [2,3,3]. + +Hence the answer is `2 + 1 + 1 = 4`. + +**Example 3:** + +**Input:** nums = [1,2,3], k = 7 + +**Output:** 0 + +**Explanation: **There exists no subsequence with sum `7`. Hence all subsequences of nums have `power = 0`. + +**Constraints:** + +* `1 <= n <= 100` +* 1 <= nums[i] <= 104 +* `1 <= k <= 100` \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/Solution.java b/src/main/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/Solution.java new file mode 100644 index 000000000..0d0381920 --- /dev/null +++ b/src/main/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/Solution.java @@ -0,0 +1,23 @@ +package g3001_3100.s3083_existence_of_a_substring_in_a_string_and_its_reverse; + +// #Easy #String #Hash_Table #2024_04_17_Time_1_ms_(99.84%)_Space_41.7_MB_(96.32%) + +public class Solution { + public boolean isSubstringPresent(String s) { + if (s.length() == 1) { + return false; + } + StringBuilder revSb = new StringBuilder(); + for (int i = s.length() - 1; i >= 0; i--) { + revSb.append(s.charAt(i)); + } + String rev = revSb.toString(); + for (int i = 0; i < s.length() - 1; i++) { + String sub = s.substring(i, i + 2); + if (rev.contains(sub)) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/readme.md b/src/main/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/readme.md new file mode 100644 index 000000000..19c5832f1 --- /dev/null +++ b/src/main/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/readme.md @@ -0,0 +1,36 @@ +3083\. Existence of a Substring in a String and Its Reverse + +Easy + +Given a string `s`, find any substring of length `2` which is also present in the reverse of `s`. + +Return `true` _if such a substring exists, and_ `false` _otherwise._ + +**Example 1:** + +**Input:** s = "leetcode" + +**Output:** true + +**Explanation:** Substring `"ee"` is of length `2` which is also present in `reverse(s) == "edocteel"`. + +**Example 2:** + +**Input:** s = "abcba" + +**Output:** true + +**Explanation:** All of the substrings of length `2` `"ab"`, `"bc"`, `"cb"`, `"ba"` are also present in `reverse(s) == "abcba"`. + +**Example 3:** + +**Input:** s = "abcd" + +**Output:** false + +**Explanation:** There is no substring of length `2` in `s`, which is also present in the reverse of `s`. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/Solution.java b/src/main/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/Solution.java new file mode 100644 index 000000000..4c5d110f2 --- /dev/null +++ b/src/main/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/Solution.java @@ -0,0 +1,15 @@ +package g3001_3100.s3084_count_substrings_starting_and_ending_with_given_character; + +// #Medium #String #Math #Counting #2024_04_17_Time_1_ms_(100.00%)_Space_44.9_MB_(30.07%) + +public class Solution { + public long countSubstrings(String s, char c) { + long count = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == c) { + count++; + } + } + return (count * (count + 1)) / 2; + } +} diff --git a/src/main/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/readme.md b/src/main/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/readme.md new file mode 100644 index 000000000..35ed10fde --- /dev/null +++ b/src/main/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/readme.md @@ -0,0 +1,26 @@ +3084\. Count Substrings Starting and Ending with Given Character + +Medium + +You are given a string `s` and a character `c`. Return _the total number of substrings of_ `s` _that start and end with_ `c`_._ + +**Example 1:** + +**Input:** s = "abada", c = "a" + +**Output:** 6 + +**Explanation:** Substrings starting and ending with `"a"` are: "**a**bada", "**aba**da", "**abada**", "ab**a**da", "ab**ada**", "abad**a**". + +**Example 2:** + +**Input:** s = "zzz", c = "z" + +**Output:** 6 + +**Explanation:** There are a total of `6` substrings in `s` and all start and end with `"z"`. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` and `c` consist only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/Solution.java b/src/main/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/Solution.java new file mode 100644 index 000000000..4d10257ab --- /dev/null +++ b/src/main/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/Solution.java @@ -0,0 +1,29 @@ +package g3001_3100.s3085_minimum_deletions_to_make_string_k_special; + +// #Medium #String #Hash_Table #Sorting #Greedy #Counting +// #2024_04_17_Time_4_ms_(100.00%)_Space_45.1_MB_(94.33%) + +public class Solution { + public int minimumDeletions(String word, int k) { + int[] arr = new int[26]; + for (int i = 0; i < word.length(); i++) { + arr[word.charAt(i) - 'a']++; + } + int min = Integer.MAX_VALUE; + for (int value : arr) { + if (value != 0) { + int u = value + k; + int res = 0; + for (int i : arr) { + if (i < value) { + res += i; + } else if (i > u) { + res += (i - u); + } + } + min = Math.min(res, min); + } + } + return min; + } +} diff --git a/src/main/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/readme.md b/src/main/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/readme.md new file mode 100644 index 000000000..b334c1fa7 --- /dev/null +++ b/src/main/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/readme.md @@ -0,0 +1,41 @@ +3085\. Minimum Deletions to Make String K-Special + +Medium + +You are given a string `word` and an integer `k`. + +We consider `word` to be **k-special** if `|freq(word[i]) - freq(word[j])| <= k` for all indices `i` and `j` in the string. + +Here, `freq(x)` denotes the frequency of the character `x` in `word`, and `|y|` denotes the absolute value of `y`. + +Return _the **minimum** number of characters you need to delete to make_ `word` **_k-special_**. + +**Example 1:** + +**Input:** word = "aabcaba", k = 0 + +**Output:** 3 + +**Explanation:** We can make `word` `0`\-special by deleting `2` occurrences of `"a"` and `1` occurrence of `"c"`. Therefore, `word` becomes equal to `"baba"` where `freq('a') == freq('b') == 2`. + +**Example 2:** + +**Input:** word = "dabdcbdcdcd", k = 2 + +**Output:** 2 + +**Explanation:** We can make `word` `2`\-special by deleting `1` occurrence of `"a"` and `1` occurrence of `"d"`. Therefore, `word` becomes equal to "bdcbdcdcd" where `freq('b') == 2`, `freq('c') == 3`, and `freq('d') == 4`. + +**Example 3:** + +**Input:** word = "aaabaaa", k = 2 + +**Output:** 1 + +**Explanation:** We can make `word` `2`\-special by deleting `1` occurrence of `"b"`. Therefore, `word` becomes equal to `"aaaaaa"` where each letter's frequency is now uniformly `6`. + +**Constraints:** + +* 1 <= word.length <= 105 +* 0 <= k <= 105 +* `word` consists only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/Solution.java b/src/main/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/Solution.java new file mode 100644 index 000000000..b44dd0448 --- /dev/null +++ b/src/main/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/Solution.java @@ -0,0 +1,57 @@ +package g3001_3100.s3086_minimum_moves_to_pick_k_ones; + +// #Hard #Array #Greedy #Prefix_Sum #Sliding_Window +// #2024_04_17_Time_4_ms_(97.63%)_Space_61_MB_(7.12%) + +public class Solution { + public long minimumMoves(int[] nums, int k, int maxChanges) { + int maxAdjLen = 0; + int n = nums.length; + int numOne = 0; + int l = 0; + int r = 0; + for (; r < n; r++) { + if (nums[r] != 1) { + maxAdjLen = Math.max(maxAdjLen, r - l); + l = r + 1; + } else { + numOne++; + } + } + maxAdjLen = Math.min(3, Math.max(maxAdjLen, r - l)); + if (maxAdjLen + maxChanges >= k) { + if (maxAdjLen >= k) { + return k - 1L; + } else { + return Math.max(0, maxAdjLen - 1) + (k - maxAdjLen) * 2L; + } + } + int[] ones = new int[numOne]; + int ind = 0; + for (int i = 0; i < n; i++) { + if (nums[i] == 1) { + ones[ind++] = i; + } + } + long[] preSum = new long[ones.length + 1]; + for (int i = 1; i < preSum.length; i++) { + preSum[i] = preSum[i - 1] + ones[i - 1]; + } + int target = k - maxChanges; + l = 0; + long res = Long.MAX_VALUE; + for (; l <= ones.length - target; l++) { + r = l + target - 1; + int mid = (l + r) / 2; + int median = ones[mid]; + long sum1 = preSum[mid + 1] - preSum[l]; + long sum2 = preSum[r + 1] - preSum[mid + 1]; + long area1 = (long) (mid - l + 1) * median; + long area2 = (long) (r - mid) * median; + long curRes = area1 - sum1 + sum2 - area2; + res = Math.min(res, curRes); + } + res += 2L * maxChanges; + return res; + } +} diff --git a/src/main/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/readme.md b/src/main/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/readme.md new file mode 100644 index 000000000..338a3b62a --- /dev/null +++ b/src/main/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/readme.md @@ -0,0 +1,48 @@ +3086\. Minimum Moves to Pick K Ones + +Hard + +You are given a binary array `nums` of length `n`, a **positive** integer `k` and a **non-negative** integer `maxChanges`. + +Alice plays a game, where the goal is for Alice to pick up `k` ones from `nums` using the **minimum** number of **moves**. When the game starts, Alice picks up any index `aliceIndex` in the range `[0, n - 1]` and stands there. If `nums[aliceIndex] == 1` , Alice picks up the one and `nums[aliceIndex]` becomes `0`(this **does not** count as a move). After this, Alice can make **any** number of **moves** (**including** **zero**) where in each move Alice must perform **exactly** one of the following actions: + +* Select any index `j != aliceIndex` such that `nums[j] == 0` and set `nums[j] = 1`. This action can be performed **at** **most** `maxChanges` times. +* Select any two adjacent indices `x` and `y` (`|x - y| == 1`) such that `nums[x] == 1`, `nums[y] == 0`, then swap their values (set `nums[y] = 1` and `nums[x] = 0`). If `y == aliceIndex`, Alice picks up the one after this move and `nums[y]` becomes `0`. + +Return _the **minimum** number of moves required by Alice to pick **exactly**_ `k` _ones_. + +**Example 1:** + +**Input:** nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1 + +**Output:** 3 + +**Explanation:** Alice can pick up `3` ones in `3` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 1`: + +* At the start of the game Alice picks up the one and `nums[1]` becomes `0`. `nums` becomes [1,**1**,1,0,0,1,1,0,0,1]. +* Select `j == 2` and perform an action of the first type. `nums` becomes [1,**0**,1,0,0,1,1,0,0,1] +* Select `x == 2` and `y == 1`, and perform an action of the second type. `nums` becomes [1,**1**,0,0,0,1,1,0,0,1]. As `y == aliceIndex`, Alice picks up the one and `nums` becomes [1,**0**,0,0,0,1,1,0,0,1]. +* Select `x == 0` and `y == 1`, and perform an action of the second type. `nums` becomes [0,**1**,0,0,0,1,1,0,0,1]. As `y == aliceIndex`, Alice picks up the one and `nums` becomes [0,**0**,0,0,0,1,1,0,0,1]. + +Note that it may be possible for Alice to pick up `3` ones using some other sequence of `3` moves. + +**Example 2:** + +**Input:** nums = [0,0,0,0], k = 2, maxChanges = 3 + +**Output:** 4 + +**Explanation:** Alice can pick up `2` ones in `4` moves, if Alice performs the following actions in each move when standing at `aliceIndex == 0`: + +* Select `j == 1` and perform an action of the first type. `nums` becomes [**0**,1,0,0]. +* Select `x == 1` and `y == 0`, and perform an action of the second type. `nums` becomes [**1**,0,0,0]. As `y == aliceIndex`, Alice picks up the one and `nums` becomes [**0**,0,0,0]. +* Select `j == 1` again and perform an action of the first type. `nums` becomes [**0**,1,0,0]. +* Select `x == 1` and `y == 0` again, and perform an action of the second type. `nums` becomes [**1**,0,0,0]. As `y == aliceIndex`, Alice picks up the one and `nums` becomes [**0**,0,0,0]. + +**Constraints:** + +* 2 <= n <= 105 +* `0 <= nums[i] <= 1` +* 1 <= k <= 105 +* 0 <= maxChanges <= 105 +* `maxChanges + sum(nums) >= k` \ No newline at end of file diff --git a/src/test/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/SolutionTest.java b/src/test/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/SolutionTest.java new file mode 100644 index 000000000..f6b57345b --- /dev/null +++ b/src/test/java/g3001_3100/s3082_find_the_sum_of_the_power_of_all_subsequences/SolutionTest.java @@ -0,0 +1,18 @@ +package g3001_3100.s3082_find_the_sum_of_the_power_of_all_subsequences; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void sumOfPower() { + assertThat(new Solution().sumOfPower(new int[] {2, 3, 3}, 5), equalTo(4)); + } + + @Test + void sumOfPower2() { + assertThat(new Solution().sumOfPower(new int[] {1, 2, 3}, 7), equalTo(0)); + } +} diff --git a/src/test/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/SolutionTest.java b/src/test/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/SolutionTest.java new file mode 100644 index 000000000..8274b0756 --- /dev/null +++ b/src/test/java/g3001_3100/s3083_existence_of_a_substring_in_a_string_and_its_reverse/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3083_existence_of_a_substring_in_a_string_and_its_reverse; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void isSubstringPresent() { + assertThat(new Solution().isSubstringPresent("leetcode"), equalTo(true)); + } + + @Test + void isSubstringPresent2() { + assertThat(new Solution().isSubstringPresent("abcba"), equalTo(true)); + } + + @Test + void isSubstringPresent3() { + assertThat(new Solution().isSubstringPresent("abcd"), equalTo(false)); + } +} diff --git a/src/test/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/SolutionTest.java b/src/test/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/SolutionTest.java new file mode 100644 index 000000000..1d41f8d7b --- /dev/null +++ b/src/test/java/g3001_3100/s3084_count_substrings_starting_and_ending_with_given_character/SolutionTest.java @@ -0,0 +1,18 @@ +package g3001_3100.s3084_count_substrings_starting_and_ending_with_given_character; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countSubstrings() { + assertThat(new Solution().countSubstrings("abada", 'a'), equalTo(6L)); + } + + @Test + void countSubstrings2() { + assertThat(new Solution().countSubstrings("zzz", 'z'), equalTo(6L)); + } +} diff --git a/src/test/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/SolutionTest.java b/src/test/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/SolutionTest.java new file mode 100644 index 000000000..6210e78dd --- /dev/null +++ b/src/test/java/g3001_3100/s3085_minimum_deletions_to_make_string_k_special/SolutionTest.java @@ -0,0 +1,23 @@ +package g3001_3100.s3085_minimum_deletions_to_make_string_k_special; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumDeletions() { + assertThat(new Solution().minimumDeletions("aabcaba", 0), equalTo(3)); + } + + @Test + void minimumDeletions2() { + assertThat(new Solution().minimumDeletions("dabdcbdcdcd", 2), equalTo(2)); + } + + @Test + void minimumDeletions3() { + assertThat(new Solution().minimumDeletions("aaabaaa", 2), equalTo(1)); + } +} diff --git a/src/test/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/SolutionTest.java b/src/test/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/SolutionTest.java new file mode 100644 index 000000000..f6e035cd5 --- /dev/null +++ b/src/test/java/g3001_3100/s3086_minimum_moves_to_pick_k_ones/SolutionTest.java @@ -0,0 +1,25 @@ +package g3001_3100.s3086_minimum_moves_to_pick_k_ones; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumMoves() { + assertThat( + new Solution().minimumMoves(new int[] {1, 1, 0, 0, 0, 1, 1, 0, 0, 1}, 3, 1), + equalTo(3L)); + } + + @Test + void minimumMoves2() { + assertThat(new Solution().minimumMoves(new int[] {0, 0, 0, 0}, 2, 3), equalTo(4L)); + } + + @Test + void minimumMoves3() { + assertThat(new Solution().minimumMoves(new int[] {1, 0, 1, 0, 1}, 3, 0), equalTo(4L)); + } +}