Skip to content

Commit 77ad805

Browse files
add 3571
1 parent 36bde2b commit 77ad805

3 files changed

Lines changed: 87 additions & 1 deletion

File tree

  • paginated_contents/algorithms/4th_thousand
  • src

‎paginated_contents/algorithms/4th_thousand/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
| 3622 | [Check Divisibility by Digit Sum and Product](https://leetcode.com/problems/check-divisibility-by-digit-sum-and-product/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3622.java) | | Easy |
7777
| 3608 | [Minimum Time for K Connected Components](https://leetcode.com/problems/minimum-time-for-k-connected-components/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3608.java) | | Medium | UnionFind
7878
| 3606 | [Coupon Code Validator](https://leetcode.com/problems/coupon-code-validator/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3606.java) | | Easy |
79-
| 3581 | [Count Odd Letters from Number](https://leetcode.com/problems/count-odd-letters-from-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3581.java) | | Easy |
79+
| 3581 | [Count Odd Letters from Number](https://leetcode.com/problems/count-odd-letters-from-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3581.java) | | Easy |
80+
| 3571 | [Find the Shortest Superstring II](https://leetcode.com/problems/find-the-shortest-superstring-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3571.java) | | Easy |
8081
| 3569 | [Count Islands With Total Value Divisible by K](https://leetcode.com/problems/count-islands-with-total-value-divisible-by-k/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3569.java) | | Medium | Union Find
8182
| 3550 | [Smallest Index With Digit Sum Equal to Index](https://leetcode.com/problems/smallest-index-with-digit-sum-equal-to-index/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3550.java) | | Easy |
8283
| 3545 | [Minimum Deletions for At Most K Distinct Characters](https://leetcode.com/problems/minimum-deletions-for-at-most-k-distinct-characters/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3545.java) | | Easy |
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3571 {
4+
public static class Solution1 {
5+
public String shortestSuperstring(String s1, String s2) {
6+
String longer = s1.length() > s2.length() ? s1 : s2;
7+
String shorter = longer.equals(s1) ? s2 : s1;
8+
if (longer.indexOf(shorter) != -1) {
9+
return longer;
10+
}
11+
//find the longest prefix of s1 that matches the longest suffix of s2
12+
String candidate = "";
13+
for (int i = s1.length(); i > 0; i--) {
14+
String prefix = s1.substring(0, i);
15+
for (int j = 0; j < s2.length(); j++) {
16+
String suffix = s2.substring(j);
17+
if (prefix.equals(suffix)) {
18+
candidate = s2 + s1.substring(i);
19+
break;
20+
}
21+
}
22+
if (!candidate.isEmpty()) {
23+
break;
24+
}
25+
}
26+
27+
//then do the other way:
28+
for (int i = s2.length(); i > 0; i--) {
29+
String prefix = s2.substring(0, i);
30+
for (int j = 0; j < s1.length(); j++) {
31+
String suffix = s1.substring(j);
32+
if (prefix.equals(suffix)) {
33+
String candidate2 = s1 + s2.substring(i);
34+
if (candidate.isEmpty()) {
35+
return candidate2;
36+
}
37+
if (candidate2.length() < candidate.length()) {
38+
return candidate2;
39+
} else {
40+
return candidate;
41+
}
42+
}
43+
}
44+
}
45+
return candidate.isEmpty() ? s1 + s2 : candidate;
46+
}
47+
}
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3571;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3571Test {
10+
private _3571.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3571.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("aaa", solution1.shortestSuperstring("aa", "aaa"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("tfj", solution1.shortestSuperstring("t", "fj"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("lkgy", solution1.shortestSuperstring("lk", "kgy"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("baba", solution1.shortestSuperstring("aba", "bab"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)