Skip to content

Commit 293bf60

Browse files
Added tasks 2206, 2207.
1 parent 8c80d81 commit 293bf60

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2201_2300.s2206_divide_array_into_equal_pairs;
2+
3+
// #Easy #Array #Hash_Table #Bit_Manipulation #Counting
4+
// #2022_06_11_Time_1_ms_(100.00%)_Space_42.2_MB_(94.00%)
5+
6+
public class Solution {
7+
public boolean divideArray(int[] nums) {
8+
int[] freq = new int[501];
9+
for (int i = 0; i < nums.length; i++) {
10+
++freq[nums[i]];
11+
}
12+
for (int f : freq) {
13+
if (f % 2 != 0) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2206\. Divide Array Into Equal Pairs
2+
3+
Easy
4+
5+
You are given an integer array `nums` consisting of `2 * n` integers.
6+
7+
You need to divide `nums` into `n` pairs such that:
8+
9+
* Each element belongs to **exactly one** pair.
10+
* The elements present in a pair are **equal**.
11+
12+
Return `true` _if nums can be divided into_ `n` _pairs, otherwise return_ `false`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [3,2,3,2,2,2]
17+
18+
**Output:** true
19+
20+
**Explanation:**
21+
22+
There are 6 elements in nums, so they should be divided into 6 / 2 = 3 pairs.
23+
24+
If nums is divided into the pairs (2, 2), (3, 3), and (2, 2), it will satisfy all the conditions.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [1,2,3,4]
29+
30+
**Output:** false
31+
32+
**Explanation:**
33+
34+
There is no way to divide nums into 4 / 2 = 2 pairs such that the pairs satisfy every condition.
35+
36+
**Constraints:**
37+
38+
* `nums.length == 2 * n`
39+
* `1 <= n <= 500`
40+
* `1 <= nums[i] <= 500`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2201_2300.s2207_maximize_number_of_subsequences_in_a_string;
2+
3+
// #Medium #String #Greedy #Prefix_Sum #2022_06_11_Time_8_ms_(100%)_Space_43.1_MB(81.70%)
4+
5+
public class Solution {
6+
public long maximumSubsequenceCount(String text, String pattern) {
7+
char first = pattern.charAt(0);
8+
char second = pattern.charAt(1);
9+
if (first == second) {
10+
long res = 0;
11+
for (char c : text.toCharArray()) {
12+
if (c == first) {
13+
res++;
14+
}
15+
}
16+
return (res * (res + 1)) / 2;
17+
}
18+
long res = 0;
19+
int firstCount = 0;
20+
int secondCount = 0;
21+
for (char c : text.toCharArray()) {
22+
if (c == first) {
23+
firstCount++;
24+
} else if (c == second) {
25+
secondCount++;
26+
res += firstCount;
27+
}
28+
}
29+
return Math.max(res + secondCount, res + firstCount);
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2207\. Maximize Number of Subsequences in a String
2+
3+
Medium
4+
5+
You are given a **0-indexed** string `text` and another **0-indexed** string `pattern` of length `2`, both of which consist of only lowercase English letters.
6+
7+
You can add **either** `pattern[0]` **or** `pattern[1]` anywhere in `text` **exactly once**. Note that the character can be added even at the beginning or at the end of `text`.
8+
9+
Return _the **maximum** number of times_ `pattern` _can occur as a **subsequence** of the modified_ `text`.
10+
11+
A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
12+
13+
**Example 1:**
14+
15+
**Input:** text = "abdcdbc", pattern = "ac"
16+
17+
**Output:** 4
18+
19+
**Explanation:**
20+
21+
If we add pattern[0] = 'a' in between text[1] and text[2], we get "ab**a**dcdbc". Now, the number of times "ac" occurs as a subsequence is 4.
22+
23+
Some other strings which have 4 subsequences "ac" after adding a character to text are "**a**abdcdbc" and "abd**a**cdbc".
24+
25+
However, strings such as "abdc**a**dbc", "abd**c**cdbc", and "abdcdbc**c**", although obtainable, have only 3 subsequences "ac" and are thus suboptimal.
26+
27+
It can be shown that it is not possible to get more than 4 subsequences "ac" by adding only one character.
28+
29+
**Example 2:**
30+
31+
**Input:** text = "aabb", pattern = "ab"
32+
33+
**Output:** 6
34+
35+
**Explanation:**
36+
37+
Some of the strings which can be obtained from text and have 6 subsequences "ab" are "**a**aabb", "aa**a**bb", and "aab**b**b".
38+
39+
**Constraints:**
40+
41+
* <code>1 <= text.length <= 10<sup>5</sup></code>
42+
* `pattern.length == 2`
43+
* `text` and `pattern` consist only of lowercase English letters.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2201_2300.s2206_divide_array_into_equal_pairs;
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 divideArray() {
11+
assertThat(new Solution().divideArray(new int[] {3, 2, 3, 2, 2, 2}), equalTo(true));
12+
}
13+
14+
@Test
15+
void divideArray2() {
16+
assertThat(new Solution().divideArray(new int[] {1, 2, 3, 4}), equalTo(false));
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2201_2300.s2207_maximize_number_of_subsequences_in_a_string;
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 maximumSubsequenceCount() {
11+
assertThat(new Solution().maximumSubsequenceCount("abdcdbc", "ac"), equalTo(4L));
12+
}
13+
14+
@Test
15+
void maximumSubsequenceCount2() {
16+
assertThat(new Solution().maximumSubsequenceCount("aabb", "ab"), equalTo(6L));
17+
}
18+
19+
@Test
20+
void maximumSubsequenceCount3() {
21+
assertThat(new Solution().maximumSubsequenceCount("abdcdbc", "aa"), equalTo(1L));
22+
}
23+
}

0 commit comments

Comments
 (0)