Skip to content

Commit 50aee45

Browse files
authored
Added task 2516
1 parent 1fc67dc commit 50aee45

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,7 @@ implementation 'com.github.javadev:leetcode-in-java:1.19'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2516 |[Take K of Each Character From Left and Right](src/main/java/g2501_2600/s2516_take_k_of_each_character_from_left_and_right/Solution.java)| Medium | String, Hash_Table, Sliding_Window | 6 | 94.24
18511852
| 2515 |[Shortest Distance to Target String in a Circular Array](src/main/java/g2501_2600/s2515_shortest_distance_to_target_string_in_a_circular_array/Solution.java)| Easy | Array, String | 1 | 62.21
18521853
| 2514 |[Count Anagrams](src/main/java/g2501_2600/s2514_count_anagrams/Solution.java)| Hard | String, Hash_Table, Math, Counting, Combinatorics | 22 | 100.00
18531854
| 2513 |[Minimize the Maximum of Two Arrays](src/main/java/g2501_2600/s2513_minimize_the_maximum_of_two_arrays/Solution.java)| Medium | Math, Binary_Search, Number_Theory | 0 | 100.00
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g2501_2600.s2516_take_k_of_each_character_from_left_and_right;
2+
3+
// #Medium #String #Hash_Table #Sliding_Window #2023_03_30_Time_6_ms_(94.24%)_Space_43.5_MB_(46.60%)
4+
5+
public class Solution {
6+
public int takeCharacters(String s, int k) {
7+
if (s.length() < 3 * k) {
8+
return -1;
9+
}
10+
int[] cnt = new int[3];
11+
char[] arr = s.toCharArray();
12+
for (char ch : arr) {
13+
cnt[ch - 'a']++;
14+
}
15+
if (cnt[0] < k || cnt[1] < k || cnt[2] < k) {
16+
return -1;
17+
}
18+
int ans = arr.length;
19+
int i = 0;
20+
int j = 0;
21+
while (j < arr.length) {
22+
cnt[arr[j] - 'a']--;
23+
if (cnt[0] >= k && cnt[1] >= k && cnt[2] >= k) {
24+
ans = Math.min(ans, arr.length - (j - i + 1));
25+
j++;
26+
} else {
27+
cnt[arr[i] - 'a']++;
28+
i++;
29+
j++;
30+
}
31+
}
32+
return ans;
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2516\. Take K of Each Character From Left and Right
2+
3+
Medium
4+
5+
You are given a string `s` consisting of the characters `'a'`, `'b'`, and `'c'` and a non-negative integer `k`. Each minute, you may take either the **leftmost** character of `s`, or the **rightmost** character of `s`.
6+
7+
Return _the **minimum** number of minutes needed for you to take **at least**_ `k` _of each character, or return_ `-1` _if it is not possible to take_ `k` _of each character._
8+
9+
**Example 1:**
10+
11+
**Input:** s = "aabaaaacaabc", k = 2
12+
13+
**Output:** 8
14+
15+
**Explanation:**
16+
17+
Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
18+
19+
Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
20+
21+
A total of 3 + 5 = 8 minutes is needed.
22+
23+
It can be proven that 8 is the minimum number of minutes needed.
24+
25+
**Example 2:**
26+
27+
**Input:** s = "a", k = 1
28+
29+
**Output:** -1
30+
31+
**Explanation:** It is not possible to take one 'b' or 'c' so return -1.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= s.length <= 10<sup>5</sup></code>
36+
* `s` consists of only the letters `'a'`, `'b'`, and `'c'`.
37+
* `0 <= k <= s.length`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2501_2600.s2516_take_k_of_each_character_from_left_and_right;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void takeCharacters() {
11+
assertThat(new Solution().takeCharacters("aabaaaacaabc", 2), equalTo(8));
12+
}
13+
14+
@Test
15+
void takeCharacters2() {
16+
assertThat(new Solution().takeCharacters("a", 1), equalTo(-1));
17+
}
18+
}

0 commit comments

Comments
 (0)