diff --git a/src/main/java/g3101_3200/s3136_valid_word/Solution.java b/src/main/java/g3101_3200/s3136_valid_word/Solution.java new file mode 100644 index 000000000..c4c41dd46 --- /dev/null +++ b/src/main/java/g3101_3200/s3136_valid_word/Solution.java @@ -0,0 +1,35 @@ +package g3101_3200.s3136_valid_word; + +// #Easy #String #2024_05_07_Time_1_ms_(99.39%)_Space_41.9_MB_(59.69%) + +public class Solution { + public boolean isValid(String word) { + if (word.length() < 3) { + return false; + } + if (word.contains("@") || word.contains("#") || word.contains("$")) { + return false; + } + char[] vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}; + char[] consonants = { + 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', + 'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', + 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z' + }; + boolean flag1 = false; + boolean flag2 = false; + for (char c : vowels) { + if (word.indexOf(c) != -1) { + flag1 = true; + break; + } + } + for (char c : consonants) { + if (word.indexOf(c) != -1) { + flag2 = true; + break; + } + } + return flag1 && flag2; + } +} diff --git a/src/main/java/g3101_3200/s3136_valid_word/readme.md b/src/main/java/g3101_3200/s3136_valid_word/readme.md new file mode 100644 index 000000000..b83d7889d --- /dev/null +++ b/src/main/java/g3101_3200/s3136_valid_word/readme.md @@ -0,0 +1,54 @@ +3136\. Valid Word + +Easy + +A word is considered **valid** if: + +* It contains a **minimum** of 3 characters. +* It contains only digits (0-9), and English letters (uppercase and lowercase). +* It includes **at least** one **vowel**. +* It includes **at least** one **consonant**. + +You are given a string `word`. + +Return `true` if `word` is valid, otherwise, return `false`. + +**Notes:** + +* `'a'`, `'e'`, `'i'`, `'o'`, `'u'`, and their uppercases are **vowels**. +* A **consonant** is an English letter that is not a vowel. + +**Example 1:** + +**Input:** word = "234Adas" + +**Output:** true + +**Explanation:** + +This word satisfies the conditions. + +**Example 2:** + +**Input:** word = "b3" + +**Output:** false + +**Explanation:** + +The length of this word is fewer than 3, and does not have a vowel. + +**Example 3:** + +**Input:** word = "a3$e" + +**Output:** false + +**Explanation:** + +This word contains a `'$'` character and does not have a consonant. + +**Constraints:** + +* `1 <= word.length <= 20` +* `word` consists of English uppercase and lowercase letters, digits, `'@'`, `'#'`, and `'$'`. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/Solution.java b/src/main/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/Solution.java new file mode 100644 index 000000000..08e2593c1 --- /dev/null +++ b/src/main/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/Solution.java @@ -0,0 +1,26 @@ +package g3101_3200.s3137_minimum_number_of_operations_to_make_word_k_periodic; + +// #Medium #String #Hash_Table #Counting #2024_05_07_Time_19_ms_(99.53%)_Space_45.5_MB_(66.25%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int minimumOperationsToMakeKPeriodic(String word, int k) { + Map map = new HashMap<>(); + int n = word.length(); + int max = 0; + for (int i = 0; i < n; i += k) { + int hash = 0; + for (int j = i; j < i + k; j++) { + int idx = word.charAt(j) - 'a'; + hash = hash * 26 + idx; + } + int count = map.getOrDefault(hash, 0); + count++; + map.put(hash, count); + max = Math.max(max, count); + } + return n / k - max; + } +} diff --git a/src/main/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/readme.md b/src/main/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/readme.md new file mode 100644 index 000000000..8511eb430 --- /dev/null +++ b/src/main/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/readme.md @@ -0,0 +1,43 @@ +3137\. Minimum Number of Operations to Make Word K-Periodic + +Medium + +You are given a string `word` of size `n`, and an integer `k` such that `k` divides `n`. + +In one operation, you can pick any two indices `i` and `j`, that are divisible by `k`, then replace the substring of length `k` starting at `i` with the substring of length `k` starting at `j`. That is, replace the substring `word[i..i + k - 1]` with the substring `word[j..j + k - 1]`. + +Return _the **minimum** number of operations required to make_ `word` _**k-periodic**_. + +We say that `word` is **k-periodic** if there is some string `s` of length `k` such that `word` can be obtained by concatenating `s` an arbitrary number of times. For example, if `word == “ababab”`, then `word` is 2-periodic for `s = "ab"`. + +**Example 1:** + +**Input:** word = "leetcodeleet", k = 4 + +**Output:** 1 + +**Explanation:** + +We can obtain a 4-periodic string by picking i = 4 and j = 0. After this operation, word becomes equal to "leetleetleet". + +**Example 2:** + +**Input:** word = "leetcoleet", k = 2 + +**Output:** 3 + +**Explanation:** + +We can obtain a 2-periodic string by applying the operations in the table below. + + i j word + 0 2 etetcoleet + 4 0 etetetleet + 6 0 etetetetet + +**Constraints:** + +* 1 <= n == word.length <= 105 +* `1 <= k <= word.length` +* `k` divides `word.length`. +* `word` consists only of lowercase English letters. diff --git a/src/main/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/Solution.java b/src/main/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/Solution.java new file mode 100644 index 000000000..7396ef46a --- /dev/null +++ b/src/main/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/Solution.java @@ -0,0 +1,56 @@ +package g3101_3200.s3138_minimum_length_of_anagram_concatenation; + +// #Medium #String #Hash_Table #Counting #2024_05_07_Time_4_ms_(84.18%)_Space_45.3_MB_(81.03%) + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Solution { + public int minAnagramLength(String s) { + int n = s.length(); + int[] sq = new int[n]; + for (int i = 0; i < s.length(); i++) { + int ch = s.charAt(i); + if (i == 0) { + sq[i] = ch * ch; + } else { + sq[i] = sq[i - 1] + ch * ch; + } + } + List factors = getAllFactorsVer2(n); + Collections.sort(factors); + for (int j = 0; j < factors.size(); j++) { + int factor = factors.get(j); + if (factor == 1) { + if (sq[0] * n == sq[n - 1]) { + return 1; + } + } else { + int sum = sq[factor - 1]; + int start = 0; + for (int i = factor - 1; i < n; i += factor) { + if (start + sum != sq[i]) { + break; + } + start += sum; + if (i == n - 1) { + return factor; + } + } + } + } + return n - 1; + } + + private List getAllFactorsVer2(int n) { + List factors = new ArrayList<>(); + for (int i = 1; i <= Math.sqrt(n); i++) { + if (n % i == 0) { + factors.add(i); + factors.add(n / i); + } + } + return factors; + } +} diff --git a/src/main/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/readme.md b/src/main/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/readme.md new file mode 100644 index 000000000..fcf2c11fe --- /dev/null +++ b/src/main/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/readme.md @@ -0,0 +1,34 @@ +3138\. Minimum Length of Anagram Concatenation + +Medium + +You are given a string `s`, which is known to be a concatenation of **anagrams** of some string `t`. + +Return the **minimum** possible length of the string `t`. + +An **anagram** is formed by rearranging the letters of a string. For example, "aab", "aba", and, "baa" are anagrams of "aab". + +**Example 1:** + +**Input:** s = "abba" + +**Output:** 2 + +**Explanation:** + +One possible string `t` could be `"ba"`. + +**Example 2:** + +**Input:** s = "cdef" + +**Output:** 4 + +**Explanation:** + +One possible string `t` could be `"cdef"`, notice that `t` can be equal to `s`. + +**Constraints:** + +* 1 <= s.length <= 105 +* `s` consist only of lowercase English letters. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3139_minimum_cost_to_equalize_array/Solution.java b/src/main/java/g3101_3200/s3139_minimum_cost_to_equalize_array/Solution.java new file mode 100644 index 000000000..04a7f5058 --- /dev/null +++ b/src/main/java/g3101_3200/s3139_minimum_cost_to_equalize_array/Solution.java @@ -0,0 +1,44 @@ +package g3101_3200.s3139_minimum_cost_to_equalize_array; + +// #Hard #Array #Greedy #Enumeration #2024_05_07_Time_1_ms_(100.00%)_Space_57.2_MB_(83.16%) + +public class Solution { + private static final int MOD = 1_000_000_007; + private static final long LMOD = MOD; + + public int minCostToEqualizeArray(int[] nums, int cost1, int cost2) { + long max = 0L; + long min = Long.MAX_VALUE; + long sum = 0L; + for (long num : nums) { + if (num > max) { + max = num; + } + if (num < min) { + min = num; + } + sum += num; + } + final int n = nums.length; + long total = max * n - sum; + // When operation one is always better: + if ((cost1 << 1) <= cost2 || n <= 2) { + return (int) (total * cost1 % LMOD); + } + // When operation two is moderately better: + long op1 = Math.max(0L, ((max - min) << 1L) - total); + long op2 = total - op1; + long result = (op1 + (op2 & 1L)) * cost1 + (op2 >> 1L) * cost2; + // When operation two is significantly better: + total += op1 / (n - 2L) * n; + op1 %= n - 2L; + op2 = total - op1; + result = Math.min(result, (op1 + (op2 & 1L)) * cost1 + (op2 >> 1L) * cost2); + // When operation two is always better: + for (int i = 0; i < 2; ++i) { + total += n; + result = Math.min(result, (total & 1L) * cost1 + (total >> 1L) * cost2); + } + return (int) (result % LMOD); + } +} diff --git a/src/main/java/g3101_3200/s3139_minimum_cost_to_equalize_array/readme.md b/src/main/java/g3101_3200/s3139_minimum_cost_to_equalize_array/readme.md new file mode 100644 index 000000000..bcfe501b3 --- /dev/null +++ b/src/main/java/g3101_3200/s3139_minimum_cost_to_equalize_array/readme.md @@ -0,0 +1,70 @@ +3139\. Minimum Cost to Equalize Array + +Hard + +You are given an integer array `nums` and two integers `cost1` and `cost2`. You are allowed to perform **either** of the following operations **any** number of times: + +* Choose an index `i` from `nums` and **increase** `nums[i]` by `1` for a cost of `cost1`. +* Choose two **different** indices `i`, `j`, from `nums` and **increase** `nums[i]` and `nums[j]` by `1` for a cost of `cost2`. + +Return the **minimum** **cost** required to make all elements in the array **equal**_._ + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** nums = [4,1], cost1 = 5, cost2 = 2 + +**Output:** 15 + +**Explanation:** + +The following operations can be performed to make the values equal: + +* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,2]`. +* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,3]`. +* Increase `nums[1]` by 1 for a cost of 5. `nums` becomes `[4,4]`. + +The total cost is 15. + +**Example 2:** + +**Input:** nums = [2,3,3,3,5], cost1 = 2, cost2 = 1 + +**Output:** 6 + +**Explanation:** + +The following operations can be performed to make the values equal: + +* Increase `nums[0]` and `nums[1]` by 1 for a cost of 1. `nums` becomes `[3,4,3,3,5]`. +* Increase `nums[0]` and `nums[2]` by 1 for a cost of 1. `nums` becomes `[4,4,4,3,5]`. +* Increase `nums[0]` and `nums[3]` by 1 for a cost of 1. `nums` becomes `[5,4,4,4,5]`. +* Increase `nums[1]` and `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,5,4,5]`. +* Increase `nums[3]` by 1 for a cost of 2. `nums` becomes `[5,5,5,5,5]`. + +The total cost is 6. + +**Example 3:** + +**Input:** nums = [3,5,3], cost1 = 1, cost2 = 3 + +**Output:** 4 + +**Explanation:** + +The following operations can be performed to make the values equal: + +* Increase `nums[0]` by 1 for a cost of 1. `nums` becomes `[4,5,3]`. +* Increase `nums[0]` by 1 for a cost of 1. `nums` becomes `[5,5,3]`. +* Increase `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,4]`. +* Increase `nums[2]` by 1 for a cost of 1. `nums` becomes `[5,5,5]`. + +The total cost is 4. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 106 +* 1 <= cost1 <= 106 +* 1 <= cost2 <= 106 \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3136_valid_word/SolutionTest.java b/src/test/java/g3101_3200/s3136_valid_word/SolutionTest.java new file mode 100644 index 000000000..8fe1dc8de --- /dev/null +++ b/src/test/java/g3101_3200/s3136_valid_word/SolutionTest.java @@ -0,0 +1,23 @@ +package g3101_3200.s3136_valid_word; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void isValid() { + assertThat(new Solution().isValid("234Adas"), equalTo(true)); + } + + @Test + void isValid2() { + assertThat(new Solution().isValid("b3"), equalTo(false)); + } + + @Test + void isValid3() { + assertThat(new Solution().isValid("a3$e"), equalTo(false)); + } +} diff --git a/src/test/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/SolutionTest.java b/src/test/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/SolutionTest.java new file mode 100644 index 000000000..dbfb54df3 --- /dev/null +++ b/src/test/java/g3101_3200/s3137_minimum_number_of_operations_to_make_word_k_periodic/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3137_minimum_number_of_operations_to_make_word_k_periodic; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumOperationsToMakeKPeriodic() { + assertThat(new Solution().minimumOperationsToMakeKPeriodic("leetcodeleet", 4), equalTo(1)); + } + + @Test + void minimumOperationsToMakeKPeriodic2() { + assertThat(new Solution().minimumOperationsToMakeKPeriodic("leetcoleet", 2), equalTo(3)); + } +} diff --git a/src/test/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/SolutionTest.java b/src/test/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/SolutionTest.java new file mode 100644 index 000000000..f57f225f2 --- /dev/null +++ b/src/test/java/g3101_3200/s3138_minimum_length_of_anagram_concatenation/SolutionTest.java @@ -0,0 +1,18 @@ +package g3101_3200.s3138_minimum_length_of_anagram_concatenation; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minAnagramLength() { + assertThat(new Solution().minAnagramLength("abba"), equalTo(2)); + } + + @Test + void minAnagramLength2() { + assertThat(new Solution().minAnagramLength("cdef"), equalTo(4)); + } +} diff --git a/src/test/java/g3101_3200/s3139_minimum_cost_to_equalize_array/SolutionTest.java b/src/test/java/g3101_3200/s3139_minimum_cost_to_equalize_array/SolutionTest.java new file mode 100644 index 000000000..5206eb90a --- /dev/null +++ b/src/test/java/g3101_3200/s3139_minimum_cost_to_equalize_array/SolutionTest.java @@ -0,0 +1,24 @@ +package g3101_3200.s3139_minimum_cost_to_equalize_array; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minCostToEqualizeArray() { + assertThat(new Solution().minCostToEqualizeArray(new int[] {4, 1}, 5, 2), equalTo(15)); + } + + @Test + void minCostToEqualizeArray2() { + assertThat( + new Solution().minCostToEqualizeArray(new int[] {2, 3, 3, 3, 5}, 2, 1), equalTo(6)); + } + + @Test + void minCostToEqualizeArray3() { + assertThat(new Solution().minCostToEqualizeArray(new int[] {3, 5, 3}, 1, 3), equalTo(4)); + } +}