Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g3801_3900.s3853_merge_close_characters;

// #Medium #String #Hash_Table #Senior #Biweekly_Contest_177
// #2026_07_27_Time_2_ms_(91.01%)_Space_44.66_MB_(52.91%)

public class Solution {
public String mergeCharacters(String s, int k) {
StringBuilder result = new StringBuilder();
result.ensureCapacity(s.length());
int[] cnt = new int[26];
for (int t = 0; t < s.length(); t++) {
char c = s.charAt(t);
int idx = c - 'a';
if (cnt[idx] > 0) {
continue;
}
result.append(c);
cnt[idx]++;
if (result.length() > k) {
char drop = result.charAt(result.length() - k - 1);
cnt[drop - 'a']--;
}
}
return result.toString();
}
}
57 changes: 57 additions & 0 deletions src/main/java/g3801_3900/s3853_merge_close_characters/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
3853\. Merge Close Characters

Medium

You are given a string `s` consisting of lowercase English letters and an integer `k`.

Two **equal** characters in the **current** string `s` are considered **close** if the distance between their indices is **at most** `k`.

When two characters are **close**, the right one merges into the left. Merges happen **one at a time**, and after each merge, the string updates until no more merges are possible.

Return the resulting string after performing all possible merges.

**Note**: If multiple merges are possible, always merge the pair with the **smallest left** index. If multiple pairs share the smallest left index, choose the pair with the **smallest right** index.

**Example 1:**

**Input:** s = "abca", k = 3

**Output:** "abc"

**Explanation:**

* Characters `'a'` at indices `i = 0` and `i = 3` are close as `3 - 0 = 3 <= k`.
* Merge them into the left `'a'` and `s = "abc"`.
* No other equal characters are close, so no further merges occur.

**Example 2:**

**Input:** s = "aabca", k = 2

**Output:** "abca"

**Explanation:**

* Characters `'a'` at indices `i = 0` and `i = 1` are close as `1 - 0 = 1 <= k`.
* Merge them into the left `'a'` and `s = "abca"`.
* Now the remaining `'a'` characters at indices `i = 0` and `i = 3` are not close as `k < 3`, so no further merges occur.

**Example 3:**

**Input:** s = "yybyzybz", k = 2

**Output:** "ybzybz"

**Explanation:**

* Characters `'y'` at indices `i = 0` and `i = 1` are close as `1 - 0 = 1 <= k`.
* Merge them into the left `'y'` and `s = "ybyzybz"`.
* Now the characters `'y'` at indices `i = 0` and `i = 2` are close as `2 - 0 = 2 <= k`.
* Merge them into the left `'y'` and `s = "ybzybz"`.
* No other equal characters are close, so no further merges occur.

**Constraints:**

* `1 <= s.length <= 100`
* `1 <= k <= s.length`
* `s` consists of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package g3801_3900.s3854_minimum_operations_to_make_array_parity_alternating;

// #Medium #Array #Greedy #Staff #Biweekly_Contest_177
// #2026_07_27_Time_9_ms_(100.00%)_Space_146.36_MB_(88.24%)

public class Solution {
public int[] makeParityAlternating(int[] nums) {
if (nums.length == 1) {
return new int[] {0, 0};
}
int zero = 0;
int one = 0;
for (int i = 0; i < nums.length; i++) {
if ((nums[i] & 1) != (i & 1)) {
zero++;
}
if ((nums[i] & 1) != ((i + 1) & 1)) {
one++;
}
}
int[] ans = new int[2];
ans[0] = Math.min(one, zero);
if (one == zero) {
ans[1] = Math.min(fun(nums, 0), fun(nums, 1));
} else if (one > zero) {
ans[1] = fun(nums, 0);
} else {
ans[1] = fun(nums, 1);
}
return ans;
}

private int fun(int[] nums, int parity) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
if ((nums[i] & 1) == ((i + parity) & 1)) {
max = Math.max(max, nums[i]);
min = Math.min(min, nums[i]);
} else {
max = Math.max(max, nums[i] - 1);
min = Math.min(min, nums[i] + 1);
}
}
return Math.max(max - min, 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
3854\. Minimum Operations to Make Array Parity Alternating

Medium

You are given an integer array `nums`.

An array is called **parity alternating** if for every index `i` where `0 <= i < n - 1`, `nums[i]` and `nums[i + 1]` have different parity (one is even and the other is odd).

In one operation, you may choose any index `i` and either increase `nums[i]` by 1 or decrease `nums[i]` by 1.

Return an integer array `answer` of length 2 where:

* `answer[0]` is the **minimum** number of operations required to make the array parity alternating.
* `answer[1]` is the **minimum** possible value of `max(nums) - min(nums)` taken over all arrays that are parity alternating and can be obtained by performing **exactly** `answer[0]` operations.

An array of length 1 is considered parity alternating.

**Example 1:**

**Input:** nums = [-2,-3,1,4]

**Output:** [2,6]

**Explanation:**

Applying the following operations:

* Increase `nums[2]` by 1, resulting in `nums = [-2, -3, 2, 4]`.
* Decrease `nums[3]` by 1, resulting in `nums = [-2, -3, 2, 3]`.

The resulting array is parity alternating, and the value of `max(nums) - min(nums) = 3 - (-3) = 6` is the minimum possible among all parity alternating arrays obtainable using exactly 2 operations.

**Example 2:**

**Input:** nums = [0,2,-2]

**Output:** [1,3]

**Explanation:**

Applying the following operation:

* Decrease `nums[1]` by 1, resulting in `nums = [0, 1, -2]`.

The resulting array is parity alternating, and the value of `max(nums) - min(nums) = 1 - (-2) = 3` is the minimum possible among all parity alternating arrays obtainable using exactly 1 operation.

**Example 3:**

**Input:** nums = [7]

**Output:** [0,0]

**Explanation:**

No operations are required. The array is already parity alternating, and the value of `max(nums) - min(nums) = 7 - 7 = 0`, which is the minimum possible.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g3801_3900.s3855_sum_of_k_digit_numbers_in_a_range;

// #Hard #Math #Divide_and_Conquer #Number_Theory #Combinatorics #Senior_Staff #Biweekly_Contest_177
// #2026_07_27_Time_2_ms_(90.59%)_Space_43.08_MB_(36.47%)

public class Solution {
private static final int MOD = 1000000007;

public int sumOfNumbers(int l, int r, int k) {
long count = r - (long) l + 1;
long sumRange = (l + r) * count / 2;
long t1 = sumRange % MOD;
long t2 = power(count, (long) k - 1);
long repunit = (power(10, k) - 1 + MOD) % MOD;
long inv9 = power(9, (long) MOD - 2);
long t3 = (repunit * inv9) % MOD;
long ans = (t1 * t2) % MOD;
ans = (ans * t3) % MOD;
return (int) ans;
}

private long power(long base, long exp) {
long res = 1;
base %= MOD;
while (exp > 0) {
if (exp % 2 == 1) {
res = (res * base) % MOD;
}
base = (base * base) % MOD;
exp /= 2;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3855\. Sum of K-Digit Numbers in a Range

Hard

You are given three integers `l`, `r`, and `k`.

Consider all possible integers consisting of **exactly** `k` digits, where each digit is chosen independently from the integer range `[l, r]` (inclusive). If 0 is included in the range, leading zeros are allowed.

Return an integer representing the **sum of all such numbers.** Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

**Input:** l = 1, r = 2, k = 2

**Output:** 66

**Explanation:**

* All numbers formed using `k = 2` digits in the range `[1, 2]` are `11, 12, 21, 22`.
* The total sum is `11 + 12 + 21 + 22 = 66`.

**Example 2:**

**Input:** l = 0, r = 1, k = 3

**Output:** 444

**Explanation:**

* All numbers formed using `k = 3` digits in the range `[0, 1]` are `000, 001, 010, 011, 100, 101, 110, 111`.
* These numbers without leading zeros are `0, 1, 10, 11, 100, 101, 110, 111`.
* The total sum is 444.

**Example 3:**

**Input:** l = 5, r = 5, k = 10

**Output:** 555555520

**Explanation:**

* 5555555555 is the only valid number consisting of `k = 10` digits in the range `[5, 5]`.
* The total sum is <code>5555555555 % (10<sup>9</sup> + 7) = 555555520</code>.

**Constraints:**

* `0 <= l <= r <= 9`
* <code>1 <= k <= 10<sup>9</sup></code>
19 changes: 19 additions & 0 deletions src/main/java/g3801_3900/s3856_trim_trailing_vowels/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3801_3900.s3856_trim_trailing_vowels;

// #Easy #String #Mid_Level #Weekly_Contest_491
// #2026_07_27_Time_1_ms_(99.24%)_Space_44.18_MB_(85.98%)

public class Solution {
public String trimTrailingVowels(String s) {
int i = s.length() - 1;
while (i >= 0
&& (s.charAt(i) == 'a'
|| s.charAt(i) == 'e'
|| s.charAt(i) == 'i'
|| s.charAt(i) == 'o'
|| s.charAt(i) == 'u')) {
i--;
}
return s.substring(0, i + 1);
}
}
44 changes: 44 additions & 0 deletions src/main/java/g3801_3900/s3856_trim_trailing_vowels/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
3856\. Trim Trailing Vowels

Easy

You are given a string `s` that consists of lowercase English letters.

Return the string obtained by removing **all** trailing **vowels** from `s`.

The **vowels** consist of the characters `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.

**Example 1:**

**Input:** s = "idea"

**Output:** "id"

**Explanation:**

Removing <code>"id<ins>**ea**</ins>"</code>, we obtain the string `"id"`.

**Example 2:**

**Input:** s = "day"

**Output:** "day"

**Explanation:**

There are no trailing vowels in the string `"day"`.

**Example 3:**

**Input:** s = "aeiou"

**Output:** ""

**Explanation:**

Removing <code>"<ins>**aeiou**</ins>"</code>, we obtain the string `""`.

**Constraints:**

* `1 <= s.length <= 100`
* `s` consists of only lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package g3801_3900.s3857_minimum_cost_to_split_into_ones;

// #Medium #Dynamic_Programming #Math #Senior #Weekly_Contest_491
// #2026_07_27_Time_0_ms_(100.00%)_Space_42.09_MB_(96.12%)

public class Solution {
public int minCost(int n) {
return n * (n - 1) / 2;
}
}
Loading