Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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,19 @@
package g1801_1900.s1876_substrings_of_size_three_with_distinct_characters;

// #Easy #String #Hash_Table #Counting #Sliding_Window
// #2022_05_11_Time_2_ms_(60.62%)_Space_42.5_MB_(33.66%)

public class Solution {
public int countGoodSubstrings(String s) {
int count = 0;
for (int i = 0; i < s.length() - 2; i++) {
String candidate = s.substring(i, i + 3);
if (candidate.charAt(0) != candidate.charAt(1)
&& candidate.charAt(0) != candidate.charAt(2)
&& candidate.charAt(1) != candidate.charAt(2)) {
count++;
}
}
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1876\. Substrings of Size Three with Distinct Characters

Easy

A string is **good** if there are no repeated characters.

Given a string `s`, return _the number of **good substrings** of length **three** in_ `s`.

Note that if there are multiple occurrences of the same substring, every occurrence should be counted.

A **substring** is a contiguous sequence of characters in a string.

**Example 1:**

**Input:** s = "xyzzaz"

**Output:** 1

**Explanation:** There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz". The only good substring of length 3 is "xyz".

**Example 2:**

**Input:** s = "aababcabc"

**Output:** 4

**Explanation:** There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab", and "abc". The good substrings are "abc", "bca", "cab", and "abc".

**Constraints:**

* `1 <= s.length <= 100`
* `s` consists of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package g1801_1900.s1878_get_biggest_three_rhombus_sums_in_a_grid;

// #Medium #Array #Math #Sorting #Matrix #Heap_Priority_Queue #Prefix_Sum
// #2022_05_11_Time_41_ms_(82.03%)_Space_54.2_MB_(58.98%)

import java.util.PriorityQueue;

public class Solution {
public int[] getBiggestThree(int[][] grid) {
int capicity = 3;
PriorityQueue<Integer> minHeap = new PriorityQueue<>();

int m = grid.length;
int n = grid[0].length;
int[][][] preSum = new int[m][n][2];
int maxLen = Math.min(m, n) / 2;

for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
addToMinHeap(minHeap, grid[r][c], capicity);
preSum[r][c][0] +=
valid(m, n, r - 1, c - 1)
? grid[r][c] + preSum[r - 1][c - 1][0]
: grid[r][c];
preSum[r][c][1] +=
valid(m, n, r - 1, c + 1)
? grid[r][c] + preSum[r - 1][c + 1][1]
: grid[r][c];
}
}

for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
for (int l = 1; l <= maxLen; l++) {
if (!valid(m, n, r - l, c - l)
|| !valid(m, n, r - l, c + l)
|| !valid(m, n, r - 2 * l, c)) {
break;
}

int rhombus = preSum[r][c][0] - preSum[r - l][c - l][0];
rhombus += preSum[r][c][1] - preSum[r - l][c + l][1];
rhombus += preSum[r - l][c - l][1] - preSum[r - 2 * l][c][1];
rhombus += preSum[r - l][c + l][0] - preSum[r - 2 * l][c][0];
rhombus += -grid[r][c] + grid[r - 2 * l][c];

addToMinHeap(minHeap, rhombus, capicity);
}
}
}

int size = minHeap.size();
int[] res = new int[size];
for (int i = size - 1; i >= 0; i--) {
res[i] = minHeap.poll();
}
return res;
}

private void addToMinHeap(PriorityQueue<Integer> minHeap, int num, int capicity) {
if (minHeap.size() == 0 || (minHeap.size() < capicity && !minHeap.contains(num))) {
minHeap.offer(num);
} else {
if (num > minHeap.peek() && !minHeap.contains(num)) {
minHeap.poll();
minHeap.offer(num);
}
}
}

private boolean valid(int m, int n, int r, int c) {
return 0 <= r && r < m && 0 <= c && c < n;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
1878\. Get Biggest Three Rhombus Sums in a Grid

Medium

You are given an `m x n` integer matrix `grid`.

A **rhombus sum** is the sum of the elements that form **the** **border** of a regular rhombus shape in `grid`. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus shapes with the corresponding colored cells that should be included in each **rhombus sum**:

![](https://assets.leetcode.com/uploads/2021/04/23/pc73-q4-desc-2.png)

Note that the rhombus can have an area of 0, which is depicted by the purple rhombus in the bottom right corner.

Return _the biggest three **distinct rhombus sums** in the_ `grid` _in **descending order**__. If there are less than three distinct values, return all of them_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/04/23/pc73-q4-ex1.png)

**Input:** grid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]]

**Output:** [228,216,211]

**Explanation:** The rhombus shapes for the three biggest distinct rhombus sums are depicted above.

- Blue: 20 + 3 + 200 + 5 = 228

- Red: 200 + 2 + 10 + 4 = 216

- Green: 5 + 200 + 4 + 2 = 211

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/04/23/pc73-q4-ex2.png)

**Input:** grid = [[1,2,3],[4,5,6],[7,8,9]]

**Output:** [20,9,8]

**Explanation:** The rhombus shapes for the three biggest distinct rhombus sums are depicted above.

- Blue: 4 + 2 + 6 + 8 = 20

- Red: 9 (area 0 rhombus in the bottom right corner)

- Green: 8 (area 0 rhombus in the bottom middle)

**Example 3:**

**Input:** grid = [[7,7,7]]

**Output:** [7]

**Explanation:** All three possible rhombus sums are the same, so return [7].

**Constraints:**

* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 50`
* <code>1 <= grid[i][j] <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g1801_1900.s1879_minimum_xor_sum_of_two_arrays;

// #Hard #Array #Dynamic_Programming #Bit_Manipulation #Bitmask
// #2022_05_11_Time_15_ms_(63.49%)_Space_42.6_MB_(53.97%)

import java.util.Arrays;

public class Solution {
public int minimumXORSum(int[] nums1, int[] nums2) {
int l = nums1.length;
int[] dp = new int[1 << l];
Arrays.fill(dp, -1);
dp[0] = 0;
return dfs(dp.length - 1, l, nums1, nums2, dp, l);
}

private int dfs(int state, int length, int[] nums1, int[] nums2, int[] dp, int totalLength) {
if (dp[state] >= 0) {
return dp[state];
}
int min = Integer.MAX_VALUE;
int currIndex = totalLength - length;
for (int i = 0, index = 0; i < length; index++) {
if (((state >> index) & 1) == 1) {
min =
Math.min(
min,
(nums2[currIndex] ^ nums1[index])
+ dfs(
state ^ (1 << index),
length - 1,
nums1,
nums2,
dp,
totalLength));
i++;
}
}
dp[state] = min;
return min;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
1879\. Minimum XOR Sum of Two Arrays

Hard

You are given two integer arrays `nums1` and `nums2` of length `n`.

The **XOR sum** of the two integer arrays is `(nums1[0] XOR nums2[0]) + (nums1[1] XOR nums2[1]) + ... + (nums1[n - 1] XOR nums2[n - 1])` (**0-indexed**).

* For example, the **XOR sum** of `[1,2,3]` and `[3,2,1]` is equal to `(1 XOR 3) + (2 XOR 2) + (3 XOR 1) = 2 + 0 + 2 = 4`.

Rearrange the elements of `nums2` such that the resulting **XOR sum** is **minimized**.

Return _the **XOR sum** after the rearrangement_.

**Example 1:**

**Input:** nums1 = [1,2], nums2 = [2,3]

**Output:** 2

**Explanation:** Rearrange `nums2` so that it becomes `[3,2]`. The XOR sum is (1 XOR 3) + (2 XOR 2) = 2 + 0 = 2.

**Example 2:**

**Input:** nums1 = [1,0,3], nums2 = [5,3,4]

**Output:** 8

**Explanation:** Rearrange `nums2` so that it becomes `[5,4,3]`. The XOR sum is (1 XOR 5) + (0 XOR 4) + (3 XOR 3) = 4 + 4 + 0 = 8.

**Constraints:**

* `n == nums1.length`
* `n == nums2.length`
* `1 <= n <= 14`
* <code>0 <= nums1[i], nums2[i] <= 10<sup>7</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g1801_1900.s1880_check_if_word_equals_summation_of_two_words;

// #Easy #String #2022_05_11_Time_2_ms_(31.97%)_Space_42.7_MB_(13.61%)

public class Solution {
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
StringBuilder sb = new StringBuilder();
int a = getSum(firstWord, sb);
sb.setLength(0);
int b = getSum(secondWord, sb);
sb.setLength(0);
int c = getSum(targetWord, sb);
return a + b == c;
}

private int getSum(String firstWord, StringBuilder sb) {
for (char c : firstWord.toCharArray()) {
sb.append(c - 'a');
}
return Integer.parseInt(sb.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
1880\. Check if Word Equals Summation of Two Words

Easy

The **letter value** of a letter is its position in the alphabet **starting from 0** (i.e. `'a' -> 0`, `'b' -> 1`, `'c' -> 2`, etc.).

The **numerical value** of some string of lowercase English letters `s` is the **concatenation** of the **letter values** of each letter in `s`, which is then **converted** into an integer.

* For example, if `s = "acb"`, we concatenate each letter's letter value, resulting in `"021"`. After converting it, we get `21`.

You are given three strings `firstWord`, `secondWord`, and `targetWord`, each consisting of lowercase English letters `'a'` through `'j'` **inclusive**.

Return `true` _if the **summation** of the **numerical values** of_ `firstWord` _and_ `secondWord` _equals the **numerical value** of_ `targetWord`_, or_ `false` _otherwise._

**Example 1:**

**Input:** firstWord = "acb", secondWord = "cba", targetWord = "cdb"

**Output:** true

**Explanation:**

The numerical value of firstWord is "acb" -> "021" -> 21.

The numerical value of secondWord is "cba" -> "210" -> 210.

The numerical value of targetWord is "cdb" -> "231" -> 231.

We return true because 21 + 210 == 231.

**Example 2:**

**Input:** firstWord = "aaa", secondWord = "a", targetWord = "aab"

**Output:** false

**Explanation:**

The numerical value of firstWord is "aaa" -> "000" -> 0.

The numerical value of secondWord is "a" -> "0" -> 0.

The numerical value of targetWord is "aab" -> "001" -> 1.

We return false because 0 + 0 != 1.

**Example 3:**

**Input:** firstWord = "aaa", secondWord = "a", targetWord = "aaaa"

**Output:** true

**Explanation:**

The numerical value of firstWord is "aaa" -> "000" -> 0.

The numerical value of secondWord is "a" -> "0" -> 0.

The numerical value of targetWord is "aaaa" -> "0000" -> 0.

We return true because 0 + 0 == 0.

**Constraints:**

* `1 <= firstWord.length,` `secondWord.length,` `targetWord.length <= 8`
* `firstWord`, `secondWord`, and `targetWord` consist of lowercase English letters from `'a'` to `'j'` **inclusive**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package g1801_1900.s1881_maximum_value_after_insertion;

// #Medium #String #Greedy #2022_05_11_Time_12_ms_(85.08%)_Space_42.8_MB_(98.31%)

public class Solution {
public String maxValue(String n, int x) {
int i = 0;
int sign = n.charAt(0) == '-' ? -1 : 1;
for (; i < n.length(); i++) {
if (n.charAt(i) != '-' && (sign * (n.charAt(i) - '0') < sign * x)) {
break;
}
}
return n.substring(0, i) + x + n.substring(i);
}
}
Loading