Skip to content

Commit 19a29ac

Browse files
authored
Added tasks 1071, 1072, 1073.
1 parent 581941f commit 19a29ac

File tree

9 files changed

+295
-0
lines changed

9 files changed

+295
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1001_1100.s1071_greatest_common_divisor_of_strings;
2+
3+
// #Easy #String #Math #2022_02_27_Time_1_ms_(82.09%)_Space_42.6_MB_(33.55%)
4+
5+
public class Solution {
6+
public String gcdOfStrings(String str1, String str2) {
7+
if (str1 == null || str2 == null) {
8+
return "";
9+
}
10+
if (str1.equals(str2)) {
11+
return str1;
12+
}
13+
int m = str1.length();
14+
int n = str2.length();
15+
if (m > n && str1.substring(0, n).equals(str2)) {
16+
return gcdOfStrings(str1.substring(n), str2);
17+
}
18+
if (n > m && str2.substring(0, m).equals(str1)) {
19+
return gcdOfStrings(str2.substring(m), str1);
20+
}
21+
return "";
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
1071\. Greatest Common Divisor of Strings
2+
3+
Easy
4+
5+
For two strings `s` and `t`, we say "`t` divides `s`" if and only if `s = t + ... + t` (i.e., `t` is concatenated with itself one or more times).
6+
7+
Given two strings `str1` and `str2`, return _the largest string_ `x` _such that_ `x` _divides both_ `str1` _and_ `str2`.
8+
9+
**Example 1:**
10+
11+
**Input:** str1 = "ABCABC", str2 = "ABC"
12+
13+
**Output:** "ABC"
14+
15+
**Example 2:**
16+
17+
**Input:** str1 = "ABABAB", str2 = "ABAB"
18+
19+
**Output:** "AB"
20+
21+
**Example 3:**
22+
23+
**Input:** str1 = "LEET", str2 = "CODE"
24+
25+
**Output:** ""
26+
27+
**Constraints:**
28+
29+
* `1 <= str1.length, str2.length <= 1000`
30+
* `str1` and `str2` consist of English uppercase letters.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g1001_1100.s1072_flip_columns_for_maximum_number_of_equal_rows;
2+
3+
// #Medium #Array #Hash_Table #Matrix #2022_02_27_Time_26_ms_(95.71%)_Space_51.4_MB_(92.86%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int maxEqualRowsAfterFlips(int[][] matrix) {
10+
/*
11+
Idea:
12+
For a given row[i], 0<=i<m, row[j], 0<=j<m and j!=i, if either of them can have
13+
all values equal after some number of flips, then
14+
row[i]==row[j] <1> or
15+
row[i]^row[j] == 111...111 <2> (xor result is a row full of '1')
16+
17+
Go further, in case<2> row[j] can turn to row[i] by flipping each column of row[j]
18+
IF assume row[i][0] is 0, then question is convert into:
19+
1> flipping each column of each row if row[i][0] is not '0',
20+
2> count the frequency of each row.
21+
The biggest number of frequencies is the answer.
22+
*/
23+
24+
// O(M*N), int M = matrix.length, N = matrix[0].length;
25+
int answer = 0;
26+
Map<String, Integer> frequency = new HashMap<>();
27+
for (int[] row : matrix) {
28+
StringBuilder rowStr = new StringBuilder();
29+
for (int c : row) {
30+
if (row[0] == 1) {
31+
rowStr.append(c == 1 ? 0 : 1);
32+
} else {
33+
rowStr.append(c);
34+
}
35+
}
36+
String key = rowStr.toString();
37+
int value = frequency.getOrDefault(key, 0) + 1;
38+
frequency.put(key, value);
39+
answer = Math.max(answer, value);
40+
}
41+
return answer;
42+
}
43+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1072\. Flip Columns For Maximum Number of Equal Rows
2+
3+
Medium
4+
5+
You are given an `m x n` binary matrix `matrix`.
6+
7+
You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from `0` to `1` or vice versa).
8+
9+
Return _the maximum number of rows that have all values equal after some number of flips_.
10+
11+
**Example 1:**
12+
13+
**Input:** matrix = [[0,1],[1,1]]
14+
15+
**Output:** 1
16+
17+
**Explanation:** After flipping no values, 1 row has all values equal.
18+
19+
**Example 2:**
20+
21+
**Input:** matrix = [[0,1],[1,0]]
22+
23+
**Output:** 2
24+
25+
**Explanation:** After flipping values in the first column, both rows have equal values.
26+
27+
**Example 3:**
28+
29+
**Input:** matrix = [[0,0,0],[0,0,1],[1,1,0]]
30+
31+
**Output:** 2
32+
33+
**Explanation:** After flipping values in the first two columns, the last two rows have equal values.
34+
35+
**Constraints:**
36+
37+
* `m == matrix.length`
38+
* `n == matrix[i].length`
39+
* `1 <= m, n <= 300`
40+
* `matrix[i][j]` is either `0` or `1`.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g1001_1100.s1073_adding_two_negabinary_numbers;
2+
3+
// #Medium #Array #Math #2022_02_27_Time_1_ms_(100.00%)_Space_42.4_MB_(57.83%)
4+
5+
public class Solution {
6+
public int[] addNegabinary(int[] arr1, int[] arr2) {
7+
int len1 = arr1.length;
8+
int len2 = arr2.length;
9+
int[] reverseArr1 = new int[len1];
10+
for (int i = len1 - 1; i >= 0; i--) {
11+
reverseArr1[len1 - i - 1] = arr1[i];
12+
}
13+
int[] reverseArr2 = new int[len2];
14+
for (int i = len2 - 1; i >= 0; i--) {
15+
reverseArr2[len2 - i - 1] = arr2[i];
16+
}
17+
int[] sumArray = new int[Math.max(len1, len2) + 2];
18+
System.arraycopy(reverseArr1, 0, sumArray, 0, len1);
19+
for (int i = 0; i < sumArray.length; i++) {
20+
if (i < len2) {
21+
sumArray[i] += reverseArr2[i];
22+
}
23+
if (sumArray[i] > 1) {
24+
sumArray[i] -= 2;
25+
sumArray[i + 1]--;
26+
} else if (sumArray[i] == -1) {
27+
sumArray[i] = 1;
28+
sumArray[i + 1]++;
29+
}
30+
}
31+
int resultLen = sumArray.length;
32+
for (int i = sumArray.length - 1; i >= 0; i--) {
33+
if (sumArray[i] == 0) {
34+
resultLen--;
35+
} else {
36+
break;
37+
}
38+
}
39+
if (resultLen == 0) {
40+
return new int[] {0};
41+
}
42+
int[] result = new int[resultLen];
43+
for (int i = resultLen - 1; i >= 0; i--) {
44+
result[resultLen - i - 1] = sumArray[i];
45+
}
46+
return result;
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
1073\. Adding Two Negabinary Numbers
2+
3+
Medium
4+
5+
Given two numbers `arr1` and `arr2` in base **\-2**, return the result of adding them together.
6+
7+
Each number is given in _array format_: as an array of 0s and 1s, from most significant bit to least significant bit. For example, `arr = [1,1,0,1]` represents the number `(-2)^3 + (-2)^2 + (-2)^0 = -3`. A number `arr` in _array, format_ is also guaranteed to have no leading zeros: either `arr == [0]` or `arr[0] == 1`.
8+
9+
Return the result of adding `arr1` and `arr2` in the same format: as an array of 0s and 1s with no leading zeros.
10+
11+
**Example 1:**
12+
13+
**Input:** arr1 = [1,1,1,1,1], arr2 = [1,0,1]
14+
15+
**Output:** [1,0,0,0,0]
16+
17+
**Explanation:** arr1 represents 11, arr2 represents 5, the output represents 16.
18+
19+
**Example 2:**
20+
21+
**Input:** arr1 = [0], arr2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** arr1 = [0], arr2 = [1]
28+
29+
**Output:** [1]
30+
31+
**Constraints:**
32+
33+
* `1 <= arr1.length, arr2.length <= 1000`
34+
* `arr1[i]` and `arr2[i]` are `0` or `1`
35+
* `arr1` and `arr2` have no leading zeros
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1001_1100.s1071_greatest_common_divisor_of_strings;
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 gcdOfStrings() {
11+
assertThat(new Solution().gcdOfStrings("ABCABC", "ABC"), equalTo("ABC"));
12+
}
13+
14+
@Test
15+
void gcdOfStrings2() {
16+
assertThat(new Solution().gcdOfStrings("ABABAB", "ABAB"), equalTo("AB"));
17+
}
18+
19+
@Test
20+
void gcdOfStrings3() {
21+
assertThat(new Solution().gcdOfStrings("LEET", "CODE"), equalTo(""));
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1001_1100.s1072_flip_columns_for_maximum_number_of_equal_rows;
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 maxEqualRowsAfterFlips() {
11+
assertThat(new Solution().maxEqualRowsAfterFlips(new int[][] {{0, 1}, {1, 1}}), equalTo(1));
12+
}
13+
14+
@Test
15+
void maxEqualRowsAfterFlips2() {
16+
assertThat(new Solution().maxEqualRowsAfterFlips(new int[][] {{0, 1}, {1, 0}}), equalTo(2));
17+
}
18+
19+
@Test
20+
void maxEqualRowsAfterFlips3() {
21+
assertThat(
22+
new Solution()
23+
.maxEqualRowsAfterFlips(new int[][] {{0, 0, 0}, {0, 0, 1}, {1, 1, 0}}),
24+
equalTo(2));
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g1001_1100.s1073_adding_two_negabinary_numbers;
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 addNegabinary() {
11+
assertThat(
12+
new Solution().addNegabinary(new int[] {1, 1, 1, 1, 1}, new int[] {1, 0, 1}),
13+
equalTo(new int[] {1, 0, 0, 0, 0}));
14+
}
15+
16+
@Test
17+
void addNegabinary2() {
18+
assertThat(
19+
new Solution().addNegabinary(new int[] {0}, new int[] {0}), equalTo(new int[] {0}));
20+
}
21+
22+
@Test
23+
void addNegabinary3() {
24+
assertThat(
25+
new Solution().addNegabinary(new int[] {0}, new int[] {1}), equalTo(new int[] {1}));
26+
}
27+
}

0 commit comments

Comments
 (0)