Skip to content

Commit 4e74d90

Browse files
authored
Added tasks 1807, 1808, 1812, 1813, 1814.
1 parent d0d79e3 commit 4e74d90

File tree

15 files changed

+495
-0
lines changed

15 files changed

+495
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1801_1900.s1807_evaluate_the_bracket_pairs_of_a_string;
2+
3+
// #Medium #Array #String #Hash_Table #2022_05_03_Time_40_ms_(80.47%)_Space_79.8_MB_(96.48%)
4+
5+
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class Solution {
10+
public String evaluate(String s, List<List<String>> knowledge) {
11+
Map<String, String> knowledgeMapper = new HashMap<>();
12+
for (List<String> pair : knowledge) {
13+
knowledgeMapper.put(pair.get(0), pair.get(1));
14+
}
15+
StringBuilder answer = new StringBuilder();
16+
int i = 0;
17+
while (i < s.length()) {
18+
char letter = s.charAt(i);
19+
if (letter == '(') {
20+
StringBuilder key = new StringBuilder();
21+
letter = s.charAt(++i);
22+
while (letter != ')') {
23+
key.append(letter);
24+
letter = s.charAt(++i);
25+
}
26+
answer.append(knowledgeMapper.getOrDefault(key.toString(), "?"));
27+
} else {
28+
answer.append(letter);
29+
}
30+
i++;
31+
}
32+
return answer.toString();
33+
}
34+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
1807\. Evaluate the Bracket Pairs of a String
2+
3+
Medium
4+
5+
You are given a string `s` that contains some bracket pairs, with each pair containing a **non-empty** key.
6+
7+
* For example, in the string `"(name)is(age)yearsold"`, there are **two** bracket pairs that contain the keys `"name"` and `"age"`.
8+
9+
You know the values of a wide range of keys. This is represented by a 2D string array `knowledge` where each <code>knowledge[i] = [key<sub>i</sub>, value<sub>i</sub>]</code> indicates that key <code>key<sub>i</sub></code> has a value of <code>value<sub>i</sub></code>.
10+
11+
You are tasked to evaluate **all** of the bracket pairs. When you evaluate a bracket pair that contains some key <code>key<sub>i</sub></code>, you will:
12+
13+
* Replace <code>key<sub>i</sub></code> and the bracket pair with the key's corresponding <code>value<sub>i</sub></code>.
14+
* If you do not know the value of the key, you will replace <code>key<sub>i</sub></code> and the bracket pair with a question mark `"?"` (without the quotation marks).
15+
16+
Each key will appear at most once in your `knowledge`. There will not be any nested brackets in `s`.
17+
18+
Return _the resulting string after evaluating **all** of the bracket pairs._
19+
20+
**Example 1:**
21+
22+
**Input:** s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]]
23+
24+
**Output:** "bobistwoyearsold"
25+
26+
**Explanation:**
27+
28+
The key "name" has a value of "bob", so replace "(name)" with "bob".
29+
30+
The key "age" has a value of "two", so replace "(age)" with "two".
31+
32+
**Example 2:**
33+
34+
**Input:** s = "hi(name)", knowledge = [["a","b"]]
35+
36+
**Output:** "hi?"
37+
38+
**Explanation:** As you do not know the value of the key "name", replace "(name)" with "?".
39+
40+
**Example 3:**
41+
42+
**Input:** s = "(a)(a)(a)aaa", knowledge = [["a","yes"]]
43+
44+
**Output:** "yesyesyesaaa"
45+
46+
**Explanation:** The same key can appear multiple times.
47+
48+
The key "a" has a value of "yes", so replace all occurrences of "(a)" with "yes".
49+
50+
Notice that the "a"s not in a bracket pair are not evaluated.
51+
52+
**Constraints:**
53+
54+
* <code>1 <= s.length <= 10<sup>5</sup></code>
55+
* <code>0 <= knowledge.length <= 10<sup>5</sup></code>
56+
* `knowledge[i].length == 2`
57+
* <code>1 <= key<sub>i</sub>.length, value<sub>i</sub>.length <= 10</code>
58+
* `s` consists of lowercase English letters and round brackets `'('` and `')'`.
59+
* Every open bracket `'('` in `s` will have a corresponding close bracket `')'`.
60+
* The key in each bracket pair of `s` will be non-empty.
61+
* There will not be any nested bracket pairs in `s`.
62+
* <code>key<sub>i</sub></code> and <code>value<sub>i</sub></code> consist of lowercase English letters.
63+
* Each <code>key<sub>i</sub></code> in `knowledge` is unique.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g1801_1900.s1808_maximize_number_of_nice_divisors;
2+
3+
// #Hard #Math #Recursion #2022_05_03_Time_1_ms_(80.77%)_Space_41_MB_(42.31%)
4+
5+
public class Solution {
6+
private long modPow(long b, int e, int m) {
7+
if (m == 1) {
8+
return 0;
9+
}
10+
if (e == 0 || b == 1) {
11+
return 1;
12+
}
13+
14+
b %= m;
15+
16+
long r = 1;
17+
while (e > 0) {
18+
if ((e & 1) == 1) {
19+
r = r * b % m;
20+
}
21+
e = e >> 1;
22+
b = b * b % m;
23+
}
24+
return r;
25+
}
26+
27+
public int maxNiceDivisors(int pf) {
28+
int mod = 1000000007;
29+
int[] st = new int[] {0, 1, 2, 3, 4, 6};
30+
return pf < 5 ? pf : (int) (modPow(3, pf / 3 - 1, mod) * st[3 + pf % 3] % mod);
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
1808\. Maximize Number of Nice Divisors
2+
3+
Hard
4+
5+
You are given a positive integer `primeFactors`. You are asked to construct a positive integer `n` that satisfies the following conditions:
6+
7+
* The number of prime factors of `n` (not necessarily distinct) is **at most** `primeFactors`.
8+
* The number of nice divisors of `n` is maximized. Note that a divisor of `n` is **nice** if it is divisible by every prime factor of `n`. For example, if `n = 12`, then its prime factors are `[2,2,3]`, then `6` and `12` are nice divisors, while `3` and `4` are not.
9+
10+
Return _the number of nice divisors of_ `n`. Since that number can be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
11+
12+
Note that a prime number is a natural number greater than `1` that is not a product of two smaller natural numbers. The prime factors of a number `n` is a list of prime numbers such that their product equals `n`.
13+
14+
**Example 1:**
15+
16+
**Input:** primeFactors = 5
17+
18+
**Output:** 6
19+
20+
**Explanation:** 200 is a valid value of n. It has 5 prime factors: [2,2,2,5,5], and it has 6 nice divisors: [10,20,40,50,100,200]. There is not other value of n that has at most 5 prime factors and more nice divisors.
21+
22+
**Example 2:**
23+
24+
**Input:** primeFactors = 8
25+
26+
**Output:** 18
27+
28+
**Constraints:**
29+
30+
* <code>1 <= primeFactors <= 10<sup>9</sup></code>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g1801_1900.s1812_determine_color_of_a_chessboard_square;
2+
3+
// #Easy #String #Math #2022_05_03_Time_1_ms_(49.36%)_Space_41.4_MB_(67.43%)
4+
5+
public class Solution {
6+
public boolean squareIsWhite(String coordinates) {
7+
char x = coordinates.charAt(0);
8+
int y = Integer.parseInt(coordinates.charAt(1) + "");
9+
switch (x) {
10+
case 'a':
11+
case 'c':
12+
case 'e':
13+
case 'g':
14+
return y % 2 == 0;
15+
default:
16+
return y % 2 != 0;
17+
}
18+
}
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1812\. Determine Color of a Chessboard Square
2+
3+
Easy
4+
5+
You are given `coordinates`, a string that represents the coordinates of a square of the chessboard. Below is a chessboard for your reference.
6+
7+
![](https://assets.leetcode.com/uploads/2021/02/19/screenshot-2021-02-20-at-22159-pm.png)
8+
9+
Return `true` _if the square is white, and_ `false` _if the square is black_.
10+
11+
The coordinate will always represent a valid chessboard square. The coordinate will always have the letter first, and the number second.
12+
13+
**Example 1:**
14+
15+
**Input:** coordinates = "a1"
16+
17+
**Output:** false
18+
19+
**Explanation:** From the chessboard above, the square with coordinates "a1" is black, so return false.
20+
21+
**Example 2:**
22+
23+
**Input:** coordinates = "h3"
24+
25+
**Output:** true
26+
27+
**Explanation:** From the chessboard above, the square with coordinates "h3" is white, so return true.
28+
29+
**Example 3:**
30+
31+
**Input:** coordinates = "c7"
32+
33+
**Output:** false
34+
35+
**Constraints:**
36+
37+
* `coordinates.length == 2`
38+
* `'a' <= coordinates[0] <= 'h'`
39+
* `'1' <= coordinates[1] <= '8'`
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g1801_1900.s1813_sentence_similarity_iii;
2+
3+
// #Medium #Array #String #Two_Pointers #2022_05_03_Time_3_ms_(40.12%)_Space_42.3_MB_(43.02%)
4+
5+
public class Solution {
6+
public boolean areSentencesSimilar(String sentence1, String sentence2) {
7+
String shorter = sentence1.length() < sentence2.length() ? sentence1 : sentence2;
8+
String longer = shorter.equals(sentence1) ? sentence2 : sentence1;
9+
String[] shortWords = shorter.split(" ");
10+
String[] longWords = longer.split(" ");
11+
int breaks = 0;
12+
int j = 0;
13+
int i = 0;
14+
while (i < shortWords.length && j < longWords.length) {
15+
if (shortWords[i].equals(longWords[j])) {
16+
j++;
17+
i++;
18+
} else {
19+
breaks++;
20+
if (breaks > 1) {
21+
break;
22+
}
23+
while (j < longWords.length && !longWords[j].equals(shortWords[i])) {
24+
j++;
25+
}
26+
}
27+
}
28+
if ((breaks == 1 && i == shortWords.length && j == longWords.length)
29+
|| (i == shortWords.length && breaks == 0)) {
30+
return true;
31+
}
32+
i = shortWords.length - 1;
33+
j = longWords.length - 1;
34+
breaks = 0;
35+
while (i >= 0 && j >= 0) {
36+
if (shortWords[i].equals(longWords[j])) {
37+
i--;
38+
j--;
39+
} else {
40+
breaks++;
41+
if (breaks > 1) {
42+
return false;
43+
}
44+
while (j >= 0 && !longWords[j].equals(shortWords[i])) {
45+
j--;
46+
}
47+
}
48+
}
49+
return (breaks == 1 && i == -1 && j == -1) || (breaks == 0 && i == -1);
50+
}
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1813\. Sentence Similarity III
2+
3+
Medium
4+
5+
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. For example, `"Hello World"`, `"HELLO"`, `"hello world hello world"` are all sentences. Words consist of **only** uppercase and lowercase English letters.
6+
7+
Two sentences `sentence1` and `sentence2` are **similar** if it is possible to insert an arbitrary sentence **(possibly empty)** inside one of these sentences such that the two sentences become equal. For example, `sentence1 = "Hello my name is Jane"` and `sentence2 = "Hello Jane"` can be made equal by inserting `"my name is"` between `"Hello"` and `"Jane"` in `sentence2`.
8+
9+
Given two sentences `sentence1` and `sentence2`, return `true` _if_ `sentence1` _and_ `sentence2` _are similar._ Otherwise, return `false`.
10+
11+
**Example 1:**
12+
13+
**Input:** sentence1 = "My name is Haley", sentence2 = "My Haley"
14+
15+
**Output:** true
16+
17+
**Explanation:** sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".
18+
19+
**Example 2:**
20+
21+
**Input:** sentence1 = "of", sentence2 = "A lot of words"
22+
23+
**Output:** false
24+
25+
**Explanation:** No single sentence can be inserted inside one of the sentences to make it equal to the other.
26+
27+
**Example 3:**
28+
29+
**Input:** sentence1 = "Eating right now", sentence2 = "Eating"
30+
31+
**Output:** true
32+
33+
**Explanation:** sentence2 can be turned to sentence1 by inserting "right now" at the end of the sentence.
34+
35+
**Constraints:**
36+
37+
* `1 <= sentence1.length, sentence2.length <= 100`
38+
* `sentence1` and `sentence2` consist of lowercase and uppercase English letters and spaces.
39+
* The words in `sentence1` and `sentence2` are separated by a single space.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g1801_1900.s1814_count_nice_pairs_in_an_array;
2+
3+
// #Medium #Array #Hash_Table #Math #Counting #2022_05_03_Time_47_ms_(83.12%)_Space_77.6_MB_(28.57%)
4+
5+
import java.util.HashMap;
6+
7+
public class Solution {
8+
9+
private int rev(int n) {
10+
int r = 0;
11+
while (n > 0) {
12+
r = r * 10 + n % 10;
13+
n = n / 10;
14+
}
15+
return r;
16+
}
17+
18+
public int countNicePairs(int[] nums) {
19+
HashMap<Integer, Integer> revMap = new HashMap<>();
20+
int cnt = 0;
21+
for (int num : nums) {
22+
int lhs = num - rev(num);
23+
int prevCnt = revMap.getOrDefault(lhs, 0);
24+
cnt += prevCnt;
25+
int mod = 1000000007;
26+
cnt = cnt % mod;
27+
revMap.put(lhs, prevCnt + 1);
28+
}
29+
30+
return cnt;
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
1814\. Count Nice Pairs in an Array
2+
3+
Medium
4+
5+
You are given an array `nums` that consists of non-negative integers. Let us define `rev(x)` as the reverse of the non-negative integer `x`. For example, `rev(123) = 321`, and `rev(120) = 21`. A pair of indices `(i, j)` is **nice** if it satisfies all of the following conditions:
6+
7+
* `0 <= i < j < nums.length`
8+
* `nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])`
9+
10+
Return _the number of nice pairs of indices_. Since that number can be too large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [42,11,1,97]
15+
16+
**Output:** 2
17+
18+
**Explanation:** The two pairs are:
19+
20+
- (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121.
21+
22+
- (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [13,10,35,24,76]
27+
28+
**Output:** 4
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
33+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)