Skip to content

Commit 9ad106b

Browse files
authored
Added tasks 1061, 1068
1 parent f1c9ed3 commit 9ad106b

File tree

7 files changed

+278
-1
lines changed

7 files changed

+278
-1
lines changed

settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'leetcode-in-java'
1+
rootProject.name = 'LeetCode-in-Java'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g1001_1100.s1061_lexicographically_smallest_equivalent_string;
2+
3+
// #Medium #String #Union_Find #2023_06_09_Time_2_ms_(93.75%)_Space_41.7_MB_(41.30%)
4+
5+
public class Solution {
6+
private int[] parent;
7+
8+
public String smallestEquivalentString(String s1, String s2, String baseStr) {
9+
parent = new int[26];
10+
int n = s1.length();
11+
StringBuilder result = new StringBuilder();
12+
for (int i = 0; i < 26; i++) {
13+
parent[i] = i;
14+
}
15+
for (int i = 0; i < n; i++) {
16+
union(s1.charAt(i) - 'a', s2.charAt(i) - 'a');
17+
}
18+
char base = 'a';
19+
for (char element : baseStr.toCharArray()) {
20+
result.append(Character.toString(base + find(element - 'a')));
21+
}
22+
return result.toString();
23+
}
24+
25+
private void union(int a, int b) {
26+
int parentA = find(a);
27+
int parentB = find(b);
28+
if (parentA != parentB) {
29+
if (parentA < parentB) {
30+
parent[parentB] = parentA;
31+
} else {
32+
parent[parentA] = parentB;
33+
}
34+
}
35+
}
36+
37+
private int find(int x) {
38+
while (parent[x] != x) {
39+
x = parent[x];
40+
}
41+
return x;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1061\. Lexicographically Smallest Equivalent String
2+
3+
Medium
4+
5+
You are given two strings of the same length `s1` and `s2` and a string `baseStr`.
6+
7+
We say `s1[i]` and `s2[i]` are equivalent characters.
8+
9+
* For example, if `s1 = "abc"` and `s2 = "cde"`, then we have `'a' == 'c'`, `'b' == 'd'`, and `'c' == 'e'`.
10+
11+
Equivalent characters follow the usual rules of any equivalence relation:
12+
13+
* **Reflexivity:** `'a' == 'a'`.
14+
* **Symmetry:** `'a' == 'b'` implies `'b' == 'a'`.
15+
* **Transitivity:** `'a' == 'b'` and `'b' == 'c'` implies `'a' == 'c'`.
16+
17+
For example, given the equivalency information from `s1 = "abc"` and `s2 = "cde"`, `"acd"` and `"aab"` are equivalent strings of `baseStr = "eed"`, and `"aab"` is the lexicographically smallest equivalent string of `baseStr`.
18+
19+
Return _the lexicographically smallest equivalent string of_ `baseStr` _by using the equivalency information from_ `s1` _and_ `s2`.
20+
21+
**Example 1:**
22+
23+
**Input:** s1 = "parker", s2 = "morris", baseStr = "parser"
24+
25+
**Output:** "makkek"
26+
27+
**Explanation:** Based on the equivalency information in s1 and s2, we can group their characters as [m,p], [a,o], [k,r,s], [e,i].
28+
29+
The characters in each group are equivalent and sorted in lexicographical order.
30+
31+
So the answer is "makkek".
32+
33+
**Example 2:**
34+
35+
**Input:** s1 = "hello", s2 = "world", baseStr = "hold"
36+
37+
**Output:** "hdld"
38+
39+
**Explanation:** Based on the equivalency information in s1 and s2, we can group their characters as [h,w], [d,e,o], [l,r].
40+
41+
So only the second letter 'o' in baseStr is changed to 'd', the answer is "hdld".
42+
43+
**Example 3:**
44+
45+
**Input:** s1 = "leetcode", s2 = "programs", baseStr = "sourcecode"
46+
47+
**Output:** "aauaaaaada"
48+
49+
**Explanation:** We group the equivalent characters in s1 and s2 as [a,o,e,r,s,c], [l,p], [g,t] and [d,m], thus all letters in baseStr except 'u' and 'd' are transformed to 'a', the answer is "aauaaaaada".
50+
51+
**Constraints:**
52+
53+
* `1 <= s1.length, s2.length, baseStr <= 1000`
54+
* `s1.length == s2.length`
55+
* `s1`, `s2`, and `baseStr` consist of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
1068\. Product Sales Analysis I
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Sales`
8+
9+
+-------------+-------+
10+
| Column Name | Type |
11+
+-------------+-------+
12+
| sale_id | int |
13+
| product_id | int |
14+
| year | int |
15+
| quantity | int |
16+
| price | int |
17+
+-------------+-------+
18+
19+
(sale_id, year) is the primary key of this table.
20+
21+
product_id is a foreign key to `Product` table.
22+
23+
Each row of this table shows a sale on the product product_id in a certain year.
24+
25+
Note that the price is per unit.
26+
27+
Table: `Product`
28+
29+
+--------------+---------+
30+
| Column Name | Type |
31+
+--------------+---------+
32+
| product_id | int |
33+
| product_name | varchar |
34+
+--------------+---------+
35+
36+
product_id is the primary key of this table.
37+
38+
Each row of this table indicates the product name of each product.
39+
40+
Write an SQL query that reports the `product_name`, `year`, and `price` for each `sale_id` in the `Sales` table.
41+
42+
Return the resulting table in **any order**.
43+
44+
The query result format is in the following example.
45+
46+
**Example 1:**
47+
48+
**Input:** Sales table:
49+
50+
+---------+------------+------+----------+-------+
51+
| sale_id | product_id | year | quantity | price |
52+
+---------+------------+------+----------+-------+
53+
| 1 | 100 | 2008 | 10 | 5000 |
54+
| 2 | 100 | 2009 | 12 | 5000 |
55+
| 7 | 200 | 2011 | 15 | 9000 |
56+
+---------+------------+------+----------+-------+
57+
58+
Product table:
59+
60+
+------------+--------------+
61+
| product_id | product_name |
62+
+------------+--------------+
63+
| 100 | Nokia |
64+
| 200 | Apple |
65+
| 300 | Samsung |
66+
+------------+--------------+
67+
68+
**Output:**
69+
70+
+--------------+-------+-------+
71+
| product_name | year | price |
72+
+--------------+-------+-------+
73+
| Nokia | 2008 | 5000 |
74+
| Nokia | 2009 | 5000 |
75+
| Apple | 2011 | 9000 |
76+
+--------------+-------+-------+
77+
78+
**Explanation:** From sale\_id = 1, we can conclude that Nokia was sold for 5000 in the year 2008. From sale\_id = 2, we can conclude that Nokia was sold for 5000 in the year 2009. From sale\_id = 7, we can conclude that Apple was sold for 9000 in the year 2011.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2023_06_09_Time_1788_ms_(91.74%)_Space_0B_(100.00%)
3+
select product.product_name, sales.sale_year, sales.price from sales
4+
left join product on product.product_id = sales.product_id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g1001_1100.s1061_lexicographically_smallest_equivalent_string;
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 smallestEquivalentString() {
11+
assertThat(
12+
new Solution().smallestEquivalentString("hello", "world", "hold"), equalTo("hdld"));
13+
}
14+
15+
@Test
16+
void smallestEquivalentString2() {
17+
assertThat(
18+
new Solution().smallestEquivalentString("parker", "morris", "parser"),
19+
equalTo("makkek"));
20+
}
21+
22+
@Test
23+
void smallestEquivalentString3() {
24+
assertThat(
25+
new Solution().smallestEquivalentString("leetcode", "programs", "sourcecode"),
26+
equalTo("aauaaaaada"));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g1001_1100.s1068_product_sales_analysis_i;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.sql.Connection;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE Sales(sale_id INTEGER, product_id INTEGER,"
24+
+ " sale_year INTEGER, quantity INTEGER, price INTEGER); "
25+
+ "INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)"
26+
+ " VALUES (1, 100, 2008, 10, 5000); "
27+
+ "INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)"
28+
+ " VALUES (2, 100, 2009, 12, 5000); "
29+
+ "INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)"
30+
+ " VALUES (7, 200, 2011, 15, 9000); "
31+
+ "CREATE TABLE Product(product_id INTEGER, product_name VARCHAR); "
32+
+ "INSERT INTO Product(product_id, product_name)"
33+
+ " VALUES (100, 'Nokia'); "
34+
+ "INSERT INTO Product(product_id, product_name)"
35+
+ " VALUES (200, 'Apple'); "
36+
+ "INSERT INTO Product(product_id, product_name)"
37+
+ " VALUES (300, 'Samsung'); ")
38+
class MysqlTest {
39+
@Test
40+
void testScript(@EmbeddedDatabase DataSource dataSource)
41+
throws SQLException, FileNotFoundException {
42+
try (final Connection connection = dataSource.getConnection()) {
43+
try (final Statement statement = connection.createStatement();
44+
final ResultSet resultSet =
45+
statement.executeQuery(
46+
new BufferedReader(
47+
new FileReader(
48+
"src/main/java/g1001_1100/"
49+
+ "s1068_product_sales_analysis_i/script.sql"))
50+
.lines()
51+
.collect(Collectors.joining("\n"))
52+
.replaceAll("#.*?\\r?\\n", ""))) {
53+
assertThat(resultSet.next(), equalTo(true));
54+
assertThat(resultSet.getNString(1), equalTo("Nokia"));
55+
assertThat(resultSet.getInt(2), equalTo(2008));
56+
assertThat(resultSet.getInt(3), equalTo(5000));
57+
assertThat(resultSet.next(), equalTo(true));
58+
assertThat(resultSet.getNString(1), equalTo("Nokia"));
59+
assertThat(resultSet.getInt(2), equalTo(2009));
60+
assertThat(resultSet.getInt(3), equalTo(5000));
61+
assertThat(resultSet.next(), equalTo(true));
62+
assertThat(resultSet.getNString(1), equalTo("Apple"));
63+
assertThat(resultSet.getInt(2), equalTo(2011));
64+
assertThat(resultSet.getInt(3), equalTo(9000));
65+
assertThat(resultSet.next(), equalTo(false));
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)