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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,9 @@
| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|--------
| 2246 |[Longest Path With Different Adjacent Characters](src/main/java/g2201_2300/s2246_longest_path_with_different_adjacent_characters)| Hard | Array, String, Depth_First_Search, Tree, Graph, Topological_Sort | 75 | 97.79
| 2241 |[Design an ATM Machine](src/main/java/g2201_2300/s2241_design_an_atm_machine)| Medium | Array, Greedy, Design | 192 | 24.16
| 2234 |[Maximum Total Beauty of the Gardens](src/main/java/g2201_2300/s2234_maximum_total_beauty_of_the_gardens)| Hard | Array, Sorting, Greedy, Binary_Search, Two_Pointers | 63 | 73.03
| 2226 |[Maximum Candies Allocated to K Children](src/main/java/g2201_2300/s2226_maximum_candies_allocated_to_k_children)| Medium | Array, Binary_Search | 46 | 78.19
| 2200 |[Find All K-Distant Indices in an Array](src/main/java/g2101_2200/s2200_find_all_k_distant_indices_in_an_array)| Easy | Array | 2 | 95.30
| 2197 |[Replace Non-Coprime Numbers in Array](src/main/java/g2101_2200/s2197_replace_non_coprime_numbers_in_array)| Hard | Array, Math, Stack, Number_Theory | 60 | 85.52
| 2196 |[Create Binary Tree From Descriptions](src/main/java/g2101_2200/s2196_create_binary_tree_from_descriptions)| Medium | Array, Hash_Table, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 85 | 76.70
Expand All @@ -1343,23 +1346,35 @@
| 2190 |[Most Frequent Number Following Key In an Array](src/main/java/g2101_2200/s2190_most_frequent_number_following_key_in_an_array)| Easy | Array, Hash_Table, Counting | 1 | 100.00
| 2188 |[Minimum Time to Finish the Race](src/main/java/g2101_2200/s2188_minimum_time_to_finish_the_race)| Hard | Array, Dynamic_Programming | 15 | 93.69
| 2187 |[Minimum Time to Complete Trips](src/main/java/g2101_2200/s2187_minimum_time_to_complete_trips)| Medium | Array, Binary_Search | 187 | 95.03
| 2186 |[Minimum Number of Steps to Make Two Strings Anagram II](src/main/java/g2101_2200/s2186_minimum_number_of_steps_to_make_two_strings_anagram_ii)| Medium | String, Hash_Table, Counting | 22 | 77.11
| 2185 |[Counting Words With a Given Prefix](src/main/java/g2101_2200/s2185_counting_words_with_a_given_prefix)| Easy | Array, String | 0 | 100.00
| 2183 |[Count Array Pairs Divisible by K](src/main/java/g2101_2200/s2183_count_array_pairs_divisible_by_k)| Hard | Array, Math, Number_Theory | 849 | 44.54
| 2182 |[Construct String With Repeat Limit](src/main/java/g2101_2200/s2182_construct_string_with_repeat_limit)| Medium | String, Greedy, Heap_Priority_Queue, Counting | 26 | 96.11
| 2181 |[Merge Nodes in Between Zeros](src/main/java/g2101_2200/s2181_merge_nodes_in_between_zeros)| Medium | Simulation, Linked_List | 6 | 96.26
| 2180 |[Count Integers With Even Digit Sum](src/main/java/g2101_2200/s2180_count_integers_with_even_digit_sum)| Easy | Math, Simulation | 0 | 100.00
| 2179 |[Count Good Triplets in an Array](src/main/java/g2101_2200/s2179_count_good_triplets_in_an_array)| Hard | Array, Binary_Search, Ordered_Set, Divide_and_Conquer, Segment_Tree, Binary_Indexed_Tree, Merge_Sort | 16 | 92.94
| 2178 |[Maximum Split of Positive Even Integers](src/main/java/g2101_2200/s2178_maximum_split_of_positive_even_integers)| Medium | Math, Greedy | 16 | 78.96
| 2177 |[Find Three Consecutive Integers That Sum to a Given Number](src/main/java/g2101_2200/s2177_find_three_consecutive_integers_that_sum_to_a_given_number)| Medium | Math, Simulation | 1 | 78.46
| 2176 |[Count Equal and Divisible Pairs in an Array](src/main/java/g2101_2200/s2176_count_equal_and_divisible_pairs_in_an_array)| Easy | Array | 4 | 78.29
| 2172 |[Maximum AND Sum of Array](src/main/java/g2101_2200/s2172_maximum_and_sum_of_array)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 21 | 87.50
| 2171 |[Removing Minimum Number of Magic Beans](src/main/java/g2101_2200/s2171_removing_minimum_number_of_magic_beans)| Medium | Array, Sorting, Prefix_Sum | 42 | 77.68
| 2170 |[Minimum Operations to Make the Array Alternating](src/main/java/g2101_2200/s2170_minimum_operations_to_make_the_array_alternating)| Medium | Array, Hash_Table, Greedy, Counting | 8 | 100.00
| 2169 |[Count Operations to Obtain Zero](src/main/java/g2101_2200/s2169_count_operations_to_obtain_zero)| Easy | Math, Simulation | 0 | 100.00
| 2167 |[Minimum Time to Remove All Cars Containing Illegal Goods](src/main/java/g2101_2200/s2167_minimum_time_to_remove_all_cars_containing_illegal_goods)| Hard | String, Dynamic_Programming | 46 | 61.00
| 2166 |[Design Bitset](src/main/java/g2101_2200/s2166_design_bitset)| Medium | Array, Hash_Table, Design | 81 | 73.38
| 2165 |[Smallest Value of the Rearranged Number](src/main/java/g2101_2200/s2165_smallest_value_of_the_rearranged_number)| Medium | Math, Sorting | 1 | 100.00
| 2164 |[Sort Even and Odd Indices Independently](src/main/java/g2101_2200/s2164_sort_even_and_odd_indices_independently)| Easy | Array, Sorting | 2 | 97.22
| 2163 |[Minimum Difference in Sums After Removal of Elements](src/main/java/g2101_2200/s2163_minimum_difference_in_sums_after_removal_of_elements)| Hard | Array, Dynamic_Programming, Heap_Priority_Queue | 298 | 57.14
| 2162 |[Minimum Cost to Set Cooking Time](src/main/java/g2101_2200/s2162_minimum_cost_to_set_cooking_time)| Medium | Math, Enumeration | 1 | 95.82
| 2161 |[Partition Array According to Given Pivot](src/main/java/g2101_2200/s2161_partition_array_according_to_given_pivot)| Medium | Array, Two_Pointers, Simulation | 7 | 72.76
| 2160 |[Minimum Sum of Four Digit Number After Splitting Digits](src/main/java/g2101_2200/s2160_minimum_sum_of_four_digit_number_after_splitting_digits)| Easy | Math, Sorting, Greedy | 1 | 78.31
| 2157 |[Groups of Strings](src/main/java/g2101_2200/s2157_groups_of_strings)| Hard | String, Bit_Manipulation, Union_Find | 451 | 93.86
| 2156 |[Find Substring With Given Hash Value](src/main/java/g2101_2200/s2156_find_substring_with_given_hash_value)| Hard | String, Sliding_Window, Hash_Function, Rolling_Hash | 37 | 36.57
| 2155 |[All Divisions With the Highest Score of a Binary Array](src/main/java/g2101_2200/s2155_all_divisions_with_the_highest_score_of_a_binary_array)| Medium | Array | 18 | 98.41
| 2154 |[Keep Multiplying Found Values by Two](src/main/java/g2101_2200/s2154_keep_multiplying_found_values_by_two)| Easy | Array, Hash_Table, Sorting, Simulation | 1 | 93.21
| 2151 |[Maximum Good People Based on Statements](src/main/java/g2101_2200/s2151_maximum_good_people_based_on_statements)| Hard | Array, Bit_Manipulation, Backtracking, Enumeration | 76 | 47.57
| 2150 |[Find All Lonely Numbers in the Array](src/main/java/g2101_2200/s2150_find_all_lonely_numbers_in_the_array)| Medium | Array, Hash_Table, Counting | 93 | 70.66
| 2149 |[Rearrange Array Elements by Sign](src/main/java/g2101_2200/s2149_rearrange_array_elements_by_sign)| Medium | Array, Two_Pointers, Simulation | 10 | 34.66
| 2148 |[Count Elements With Strictly Smaller and Greater Elements](src/main/java/g2101_2200/s2148_count_elements_with_strictly_smaller_and_greater_elements)| Easy | Array, Sorting | 0 | 100.00
| 2147 |[Number of Ways to Divide a Long Corridor](src/main/java/g2101_2200/s2147_number_of_ways_to_divide_a_long_corridor)| Hard | String, Dynamic_Programming, Math | 54 | 62.96
| 2146 |[K Highest Ranked Items Within a Price Range](src/main/java/g2101_2200/s2146_k_highest_ranked_items_within_a_price_range)| Medium | Array, Sorting, Breadth_First_Search, Matrix, Heap_Priority_Queue | 81 | 88.84
Expand All @@ -1378,7 +1393,7 @@
| 2130 |[Maximum Twin Sum of a Linked List](src/main/java/g2101_2200/s2130_maximum_twin_sum_of_a_linked_list)| Medium | Two_Pointers, Stack, Linked_List | 9 | 57.92
| 2129 |[Capitalize the Title](src/main/java/g2101_2200/s2129_capitalize_the_title)| Easy | String | 2 | 94.22
| 2127 |[Maximum Employees to Be Invited to a Meeting](src/main/java/g2101_2200/s2127_maximum_employees_to_be_invited_to_a_meeting)| Hard | Depth_First_Search, Graph, Topological_Sort | 37 | 85.71
| 2126 |[Destroying Asteroids](src/main/java/g2101_2200/s2126_destroying_asteroids)| Medium | Array, Sorting, Greedy | 25 | 83.82
| 2126 |[Destroying Asteroids](src/main/java/g2101_2200/s2126_destroying_asteroids)| Medium | Array, Sorting, Greedy | 6 | 99.27
| 2125 |[Number of Laser Beams in a Bank](src/main/java/g2101_2200/s2125_number_of_laser_beams_in_a_bank)| Medium | Array, String, Math, Matrix | 19 | 76.00
| 2124 |[Check if All A's Appears Before All B's](src/main/java/g2101_2200/s2124_check_if_all_as_appears_before_all_bs)| Easy | String | 1 | 73.82
| 2122 |[Recover the Original Array](src/main/java/g2101_2200/s2122_recover_the_original_array)| Hard | Array, Hash_Table, Sorting, Enumeration | 19 | 89.04
Expand Down
40 changes: 31 additions & 9 deletions src/main/java/g2101_2200/s2126_destroying_asteroids/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,41 @@ This is less than 23, so a collision would not destroy the last asteroid.
## Solution

```java
import java.util.Arrays;

public class Solution {
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
Arrays.sort(asteroids);
long m = mass;
for (int ele : asteroids) {
if (m < ele) {
return false;
return helper(mass, 0, asteroids);
}

private boolean helper(long mass, int startIndex, int[] asteroids) {
int smallOrEqualIndex = partition(mass, startIndex, asteroids);
if (smallOrEqualIndex < startIndex) {
return false;
}
if (smallOrEqualIndex >= asteroids.length - 1) {
return true;
}
for (int i = startIndex; i <= smallOrEqualIndex; ++i) {
mass += asteroids[i];
}
return helper(mass, ++smallOrEqualIndex, asteroids);
}

private int partition(long mass, int startIndex, int[] asteroids) {
int length = asteroids.length;
int smallOrEqualIndex = startIndex - 1;
for (int i = startIndex; i < length; ++i) {
if (asteroids[i] <= mass) {
smallOrEqualIndex++;
swap(asteroids, i, smallOrEqualIndex);
}
m += ele;
}
return true;
return smallOrEqualIndex;
}

private void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## 2149\. Rearrange Array Elements by Sign

Medium

You are given a **0-indexed** integer array `nums` of **even** length consisting of an **equal** number of positive and negative integers.

You should **rearrange** the elements of `nums` such that the modified array follows the given conditions:

1. Every **consecutive pair** of integers have **opposite signs**.
2. For all integers with the same sign, the **order** in which they were present in `nums` is **preserved**.
3. The rearranged array begins with a positive integer.

Return _the modified array after rearranging the elements to satisfy the aforementioned conditions_.

**Example 1:**

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

**Output:** [3,-2,1,-5,2,-4]

**Explanation:** The positive integers in nums are [3,1,2].

The negative integers are [-2,-5,-4].

The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].

Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.

**Example 2:**

**Input:** nums = [-1,1]

**Output:** [1,-1]

**Explanation:** 1 is the only positive integer and -1 the only negative integer in nums.

So nums is rearranged to [1,-1].

**Constraints:**

* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
* `nums.length` is **even**
* <code>1 <= |nums[i]| <= 10<sup>5</sup></code>
* `nums` consists of **equal** number of positive and negative integers.

## Solution

```java
public class Solution {
public int[] rearrangeArray(int[] nums) {
int[] negatives = new int[nums.length / 2];
int[] positives = new int[nums.length / 2];
int[] result = new int[nums.length];
int pPtr = 0;
int nPtr = 0;
int rPtr = 0;
for (int num : nums) {
if (num > 0) {
positives[pPtr++] = num;
} else {
negatives[nPtr++] = num;
}
}
pPtr = 0;
nPtr = 0;
while (pPtr < positives.length && nPtr < negatives.length) {
result[rPtr++] = positives[pPtr++];
result[rPtr++] = negatives[nPtr++];
}
return result;
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## 2156\. Find Substring With Given Hash Value

Hard

The hash of a **0-indexed** string `s` of length `k`, given integers `p` and `m`, is computed using the following function:

* <code>hash(s, p, m) = (val(s[0]) * p<sup>0</sup> + val(s[1]) * p<sup>1</sup> + ... + val(s[k-1]) * p<sup>k-1</sup>) mod m</code>.

Where `val(s[i])` represents the index of `s[i]` in the alphabet from `val('a') = 1` to `val('z') = 26`.

You are given a string `s` and the integers `power`, `modulo`, `k`, and `hashValue.` Return `sub`, _the **first** **substring** of_ `s` _of length_ `k` _such that_ `hash(sub, power, modulo) == hashValue`.

The test cases will be generated such that an answer always **exists**.

A **substring** is a contiguous non-empty sequence of characters within a string.

**Example 1:**

**Input:** s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0

**Output:** "ee"

**Explanation:** The hash of "ee" can be computed to be hash("ee", 7, 20) = (5 \* 1 + 5 \* 7) mod 20 = 40 mod 20 = 0. "ee" is the first substring of length 2 with hashValue 0. Hence, we return "ee".

**Example 2:**

**Input:** s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32

**Output:** "fbx"

**Explanation:** The hash of "fbx" can be computed to be hash("fbx", 31, 100) = (6 \* 1 + 2 \* 31 + 24 \* 31<sup>2</sup>) mod 100 = 23132 mod 100 = 32.

The hash of "bxz" can be computed to be hash("bxz", 31, 100) = (2 \* 1 + 24 \* 31 + 26 \* 31<sup>2</sup>) mod 100 = 25732 mod 100 = 32. "fbx" is the first substring of length 3 with hashValue 32. Hence, we return "fbx".

Note that "bxz" also has a hash of 32 but it appears later than "fbx".

**Constraints:**

* <code>1 <= k <= s.length <= 2 * 10<sup>4</sup></code>
* <code>1 <= power, modulo <= 10<sup>9</sup></code>
* `0 <= hashValue < modulo`
* `s` consists of lowercase English letters only.
* The test cases are generated such that an answer always **exists**.

## Solution

```java
public class Solution {
public String subStrHash(String s, int power, int modulo, int k, int hashValue) {
long mul1 = 1;
int times = k - 1;
while (times-- > 0) {
mul1 = mul1 * power % modulo;
}
int index = -1;
long hash = 0;
int end = s.length() - 1;
for (int i = s.length() - 1; i >= 0; i--) {
int val = s.charAt(i) - 96;
hash = (hash * power % modulo + val) % modulo;
if (end - i + 1 == k) {
if (hash == hashValue) {
index = i;
}
hash = (hash - (s.charAt(end) - 96) * mul1 % modulo + modulo) % modulo;
end--;
}
}
return s.substring(index, index + k);
}
}
```
123 changes: 123 additions & 0 deletions src/main/java/g2101_2200/s2157_groups_of_strings/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
## 2157\. Groups of Strings

Hard

You are given a **0-indexed** array of strings `words`. Each string consists of **lowercase English letters** only. No letter occurs more than once in any string of `words`.

Two strings `s1` and `s2` are said to be **connected** if the set of letters of `s2` can be obtained from the set of letters of `s1` by any **one** of the following operations:

* Adding exactly one letter to the set of the letters of `s1`.
* Deleting exactly one letter from the set of the letters of `s1`.
* Replacing exactly one letter from the set of the letters of `s1` with any letter, **including** itself.

The array `words` can be divided into one or more non-intersecting **groups**. A string belongs to a group if any **one** of the following is true:

* It is connected to **at least one** other string of the group.
* It is the **only** string present in the group.

Note that the strings in `words` should be grouped in such a manner that a string belonging to a group cannot be connected to a string present in any other group. It can be proved that such an arrangement is always unique.

Return _an array_ `ans` _of size_ `2` _where:_

* `ans[0]` _is the **maximum number** of groups_ `words` _can be divided into, and_
* `ans[1]` _is the **size of the largest** group_.

**Example 1:**

**Input:** words = ["a","b","ab","cde"]

**Output:** [2,3]

**Explanation:**

- words[0] can be used to obtain words[1] (by replacing 'a' with 'b'), and words[2] (by adding 'b'). So words[0] is connected to words[1] and words[2].

- words[1] can be used to obtain words[0] (by replacing 'b' with 'a'), and words[2] (by adding 'a'). So words[1] is connected to words[0] and words[2].

- words[2] can be used to obtain words[0] (by deleting 'b'), and words[1] (by deleting 'a'). So words[2] is connected to words[0] and words[1].

- words[3] is not connected to any string in words.

Thus, words can be divided into 2 groups ["a","b","ab"] and ["cde"]. The size of the largest group is 3.

**Example 2:**

**Input:** words = ["a","ab","abc"]

**Output:** [1,3]

**Explanation:**

- words[0] is connected to words[1].

- words[1] is connected to words[0] and words[2].

- words[2] is connected to words[1].

Since all strings are connected to each other, they should be grouped together.

Thus, the size of the largest group is 3.

**Constraints:**

* <code>1 <= words.length <= 2 * 10<sup>4</sup></code>
* `1 <= words[i].length <= 26`
* `words[i]` consists of lowercase English letters only.
* No letter occurs more than once in `words[i]`.

## Solution

```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Solution {
public int[] groupStrings(String[] words) {
HashMap<Integer, Integer> map = new HashMap<>();
for (String word : words) {
int bitmask = 0;
for (char ch : word.toCharArray()) {
bitmask |= (1 << (ch - 'a'));
}
map.put(bitmask, map.getOrDefault(bitmask, 0) + 1);
}
List<Integer> keyset = new ArrayList<>();
for (Integer key : map.keySet()) {
keyset.add(key);
}
int totalGroups = 0;
int maxSize = 0;
for (Integer key : keyset) {
if (!map.containsKey(key)) {
continue;
}
totalGroups++;
int size = dfs(key, map);
maxSize = Math.max(size, maxSize);
}
return new int[] {totalGroups, maxSize};
}

private int dfs(Integer key, HashMap<Integer, Integer> map) {
if (!map.containsKey(key)) {
return 0;
}
int size = map.get(key);
map.remove(key);
for (int i = 0; i < 26; i++) {
size += dfs((key ^ (1 << i)), map);
}
for (int i = 0; i < 26; i++) {
if ((key & (1 << i)) > 0) {
for (int j = 0; j < 26; j++) {
if ((key & (1 << j)) == 0) {
size += dfs((key ^ (1 << i) ^ (1 << j)), map);
}
}
}
}
return size;
}
}
```
Loading