Skip to content

Added tasks 3136-3139 #1755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/main/java/g3101_3200/s3136_valid_word/Solution.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
54 changes: 54 additions & 0 deletions src/main/java/g3101_3200/s3136_valid_word/readme.md
Original file line number Diff line number Diff line change
@@ -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 `'$'`.
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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:**

* <code>1 <= n == word.length <= 10<sup>5</sup></code>
* `1 <= k <= word.length`
* `k` divides `word.length`.
* `word` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> getAllFactorsVer2(int n) {
List<Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consist only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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** <code>10<sup>9</sup> + 7</code>.

**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:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
* <code>1 <= cost1 <= 10<sup>6</sup></code>
* <code>1 <= cost2 <= 10<sup>6</sup></code>
23 changes: 23 additions & 0 deletions src/test/java/g3101_3200/s3136_valid_word/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading
Loading