Skip to content

Commit 642b5ba

Browse files
authored
Added tasks 1074, 1078, 1079.
1 parent 28d1f66 commit 642b5ba

File tree

9 files changed

+259
-0
lines changed

9 files changed

+259
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1001_1100.s1074_number_of_submatrices_that_sum_to_target;
2+
3+
// #Hard #Array #Hash_Table #Matrix #Prefix_Sum
4+
// #2022_02_26_Time_171_ms_(68.35%)_Space_117.8_MB_(5.54%)
5+
6+
import java.util.HashMap;
7+
8+
public class Solution {
9+
public int numSubmatrixSumTarget(int[][] matrix, int target) {
10+
int rows = matrix.length;
11+
int columns = matrix[0].length;
12+
for (int i = 0; i < rows; i++) {
13+
for (int j = 1; j < columns; j++) {
14+
matrix[i][j] += matrix[i][j - 1];
15+
}
16+
}
17+
HashMap<Integer, Integer> sumMap = new HashMap<>();
18+
int cur;
19+
int res = 0;
20+
for (int i = 0; i < columns; i++) {
21+
for (int j = i; j < columns; j++) {
22+
sumMap.clear();
23+
sumMap.put(0, 1);
24+
cur = 0;
25+
for (int[] ints : matrix) {
26+
cur += ints[j] - (i > 0 ? ints[i - 1] : 0);
27+
res += sumMap.getOrDefault(cur - target, 0);
28+
sumMap.put(cur, sumMap.getOrDefault(cur, 0) + 1);
29+
}
30+
}
31+
}
32+
return res;
33+
}
34+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1074\. Number of Submatrices That Sum to Target
2+
3+
Hard
4+
5+
Given a `matrix` and a `target`, return the number of non-empty submatrices that sum to target.
6+
7+
A submatrix `x1, y1, x2, y2` is the set of all cells `matrix[x][y]` with `x1 <= x <= x2` and `y1 <= y <= y2`.
8+
9+
Two submatrices `(x1, y1, x2, y2)` and `(x1', y1', x2', y2')` are different if they have some coordinate that is different: for example, if `x1 != x1'`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg)
14+
15+
**Input:** matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
16+
17+
**Output:** 4
18+
19+
**Explanation:** The four 1x1 submatrices that only contain 0.
20+
21+
**Example 2:**
22+
23+
**Input:** matrix = [[1,-1],[-1,1]], target = 0
24+
25+
**Output:** 5
26+
27+
**Explanation:** The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
28+
29+
**Example 3:**
30+
31+
**Input:** matrix = [[904]], target = 0
32+
33+
**Output:** 0
34+
35+
**Constraints:**
36+
37+
* `1 <= matrix.length <= 100`
38+
* `1 <= matrix[0].length <= 100`
39+
* `-1000 <= matrix[i] <= 1000`
40+
* `-10^8 <= target <= 10^8`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g1001_1100.s1078_occurrences_after_bigram;
2+
3+
// #Easy #String #2022_02_26_Time_0_ms_(100.00%)_Space_40.4_MB_(48.38%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public String[] findOcurrences(String text, String first, String second) {
10+
List<String> list = new ArrayList<>();
11+
String[] str = text.split(" ");
12+
for (int i = 0; i < str.length; i++) {
13+
if (str[i].equals(first) && str.length - 1 >= i + 2 && str[i + 1].equals(second)) {
14+
list.add(str[i + 2]);
15+
}
16+
}
17+
String[] s = new String[list.size()];
18+
int j = 0;
19+
for (String ele : list) {
20+
s[j++] = ele;
21+
}
22+
return s;
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
1078\. Occurrences After Bigram
2+
3+
Easy
4+
5+
Given two strings `first` and `second`, consider occurrences in some text of the form `"first second third"`, where `second` comes immediately after `first`, and `third` comes immediately after `second`.
6+
7+
Return _an array of all the words_ `third` _for each occurrence of_ `"first second third"`.
8+
9+
**Example 1:**
10+
11+
**Input:** text = "alice is a good girl she is a good student", first = "a", second = "good"
12+
13+
**Output:** ["girl","student"]
14+
15+
**Example 2:**
16+
17+
**Input:** text = "we will we will rock you", first = "we", second = "will"
18+
19+
**Output:** ["we","rock"]
20+
21+
**Constraints:**
22+
23+
* `1 <= text.length <= 1000`
24+
* `text` consists of lowercase English letters and spaces.
25+
* All the words in `text` a separated by **a single space**.
26+
* `1 <= first.length, second.length <= 10`
27+
* `first` and `second` consist of lowercase English letters.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g1001_1100.s1079_letter_tile_possibilities;
2+
3+
// #Medium #String #Backtracking #2022_02_26_Time_3_ms_(86.86%)_Space_42.3_MB_(51.05%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
private int count;
9+
10+
public int numTilePossibilities(String tiles) {
11+
count = 0;
12+
char[] chars = tiles.toCharArray();
13+
Arrays.sort(chars);
14+
boolean[] visited = new boolean[chars.length];
15+
dfs(chars, 0, visited);
16+
return count;
17+
}
18+
19+
private void dfs(char[] chars, int length, boolean[] visited) {
20+
if (length == chars.length) {
21+
return;
22+
}
23+
for (int i = 0; i < chars.length; i++) {
24+
if (visited[i] || i - 1 >= 0 && chars[i] == chars[i - 1] && !visited[i - 1]) {
25+
continue;
26+
}
27+
count++;
28+
visited[i] = true;
29+
dfs(chars, length + 1, visited);
30+
visited[i] = false;
31+
}
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
1079\. Letter Tile Possibilities
2+
3+
Medium
4+
5+
You have `n` `tiles`, where each tile has one letter `tiles[i]` printed on it.
6+
7+
Return _the number of possible non-empty sequences of letters_ you can make using the letters printed on those `tiles`.
8+
9+
**Example 1:**
10+
11+
**Input:** tiles = "AAB"
12+
13+
**Output:** 8
14+
15+
**Explanation:** The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
16+
17+
**Example 2:**
18+
19+
**Input:** tiles = "AAABBC"
20+
21+
**Output:** 188
22+
23+
**Example 3:**
24+
25+
**Input:** tiles = "V"
26+
27+
**Output:** 1
28+
29+
**Constraints:**
30+
31+
* `1 <= tiles.length <= 7`
32+
* `tiles` consists of uppercase English letters.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1001_1100.s1074_number_of_submatrices_that_sum_to_target;
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 numSubmatrixSumTarget() {
11+
assertThat(
12+
new Solution()
13+
.numSubmatrixSumTarget(new int[][] {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}, 0),
14+
equalTo(4));
15+
}
16+
17+
@Test
18+
void numSubmatrixSumTarget2() {
19+
assertThat(
20+
new Solution().numSubmatrixSumTarget(new int[][] {{1, -1}, {-1, 1}}, 0),
21+
equalTo(5));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1001_1100.s1078_occurrences_after_bigram;
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 findOcurrences() {
11+
assertThat(
12+
new Solution()
13+
.findOcurrences("alice is a good girl she is a good student", "a", "good"),
14+
equalTo(new String[] {"girl", "student"}));
15+
}
16+
17+
@Test
18+
void findOcurrences2() {
19+
assertThat(
20+
new Solution().findOcurrences("we will we will rock you", "we", "will"),
21+
equalTo(new String[] {"we", "rock"}));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1001_1100.s1079_letter_tile_possibilities;
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 numTilePossibilities() {
11+
assertThat(new Solution().numTilePossibilities("AAB"), equalTo(8));
12+
}
13+
14+
@Test
15+
void numTilePossibilities2() {
16+
assertThat(new Solution().numTilePossibilities("AAABBC"), equalTo(188));
17+
}
18+
19+
@Test
20+
void numTilePossibilities3() {
21+
assertThat(new Solution().numTilePossibilities("V"), equalTo(1));
22+
}
23+
}

0 commit comments

Comments
 (0)