Skip to content

Commit b6e56ed

Browse files
authored
Added tasks 1047, 1048.
1 parent e6d4a7d commit b6e56ed

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1001_1100.s1047_remove_all_adjacent_duplicates_in_string;
2+
3+
// #Easy #String #Stack #2022_02_28_Time_3_ms_(99.99%)_Space_42.7_MB_(84.52%)
4+
5+
public class Solution {
6+
public String removeDuplicates(String s) {
7+
if (s.length() == 1) {
8+
return s;
9+
}
10+
char[] array = s.toCharArray();
11+
int length = array.length;
12+
int fast = 0;
13+
int slow = 0;
14+
while (fast < length) {
15+
if (slow == 0 || array[fast] != array[slow - 1]) {
16+
array[slow++] = array[fast++];
17+
} else {
18+
if (array[fast] == array[slow - 1]) {
19+
fast++;
20+
}
21+
slow--;
22+
}
23+
}
24+
return new String(array, 0, slow);
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
1047\. Remove All Adjacent Duplicates In String
2+
3+
Easy
4+
5+
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them.
6+
7+
We repeatedly make **duplicate removals** on `s` until we no longer can.
8+
9+
Return _the final string after all such duplicate removals have been made_. It can be proven that the answer is **unique**.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abbaca"
14+
15+
**Output:** "ca"
16+
17+
**Explanation:**
18+
19+
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.
20+
21+
The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
22+
23+
**Example 2:**
24+
25+
**Input:** s = "azxxzy"
26+
27+
**Output:** "ay"
28+
29+
**Constraints:**
30+
31+
* <code>1 <= s.length <= 10<sup>5</sup></code>
32+
* `s` consists of lowercase English letters.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g1001_1100.s1048_longest_string_chain;
2+
3+
// #Medium #Array #String #Hash_Table #Dynamic_Programming #Two_Pointers
4+
// #2022_02_28_Time_23_ms_(97.92%)_Space_46.6_MB_(66.36%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
@SuppressWarnings("unchecked")
12+
public class Solution {
13+
public int longestStrChain(String[] words) {
14+
List[] lenStr = new List[20];
15+
for (String word : words) {
16+
int len = word.length();
17+
if (lenStr[len] == null) {
18+
lenStr[len] = new ArrayList<String>();
19+
}
20+
lenStr[len].add(word);
21+
}
22+
Map<String, Integer> longest = new HashMap<>();
23+
int max = 0;
24+
for (String s : words) {
25+
max = Math.max(findLongest(s, lenStr, longest), max);
26+
}
27+
return max;
28+
}
29+
30+
public int findLongest(String word, List<String>[] lenStr, Map<String, Integer> longest) {
31+
if (longest.containsKey(word)) {
32+
return longest.get(word);
33+
}
34+
int len = word.length();
35+
List<String> words = lenStr[len + 1];
36+
if (words == null) {
37+
longest.put(word, 1);
38+
return 1;
39+
}
40+
int max = 0;
41+
int i;
42+
int j;
43+
for (String w : words) {
44+
i = 0;
45+
j = 0;
46+
while (i < len && j - i <= 1) {
47+
if (word.charAt(i) == w.charAt(j++)) {
48+
++i;
49+
}
50+
}
51+
if (j - i <= 1) {
52+
max = Math.max(findLongest(w, lenStr, longest), max);
53+
}
54+
}
55+
++max;
56+
longest.put(word, max);
57+
return max;
58+
}
59+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1048\. Longest String Chain
2+
3+
Medium
4+
5+
You are given an array of `words` where each word consists of lowercase English letters.
6+
7+
<code>word<sub>A</sub></code> is a **predecessor** of <code>word<sub>B</sub></code> if and only if we can insert **exactly one** letter anywhere in <code>word<sub>A</sub></code> **without changing the order of the other characters** to make it equal to <code>word<sub>B</sub></code>.
8+
9+
* For example, `"abc"` is a **predecessor** of `"abac"`, while `"cba"` is not a **predecessor** of `"bcad"`.
10+
11+
A **word chain** is a sequence of words <code>[word<sub>1</sub>, word<sub>2</sub>, ..., word<sub>k</sub>]</code> with `k >= 1`, where <code>word<sub>1</sub></code> is a **predecessor** of <code>word<sub>2</sub></code>, <code>word<sub>2</sub></code> is a **predecessor** of <code>word<sub>3</sub></code>, and so on. A single word is trivially a **word chain** with `k == 1`.
12+
13+
Return _the **length** of the **longest possible word chain** with words chosen from the given list of_ `words`.
14+
15+
**Example 1:**
16+
17+
**Input:** words = ["a","b","ba","bca","bda","bdca"]
18+
19+
**Output:** 4
20+
21+
**Explanation:**: One of the longest word chains is ["a","ba","bda","bdca"].
22+
23+
**Example 2:**
24+
25+
**Input:** words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
26+
27+
**Output:** 5
28+
29+
**Explanation:** All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].
30+
31+
**Example 3:**
32+
33+
**Input:** words = ["abcd","dbqca"]
34+
35+
**Output:** 1
36+
37+
**Explanation:** The trivial word chain ["abcd"] is one of the longest word chains. ["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
38+
39+
**Constraints:**
40+
41+
* `1 <= words.length <= 1000`
42+
* `1 <= words[i].length <= 16`
43+
* `words[i]` only consists of lowercase English letters.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g1001_1100.s1047_remove_all_adjacent_duplicates_in_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 removeDuplicates() {
11+
assertThat(new Solution().removeDuplicates("abbaca"), equalTo("ca"));
12+
}
13+
14+
@Test
15+
void removeDuplicates2() {
16+
assertThat(new Solution().removeDuplicates("azxxzy"), equalTo("ay"));
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g1001_1100.s1048_longest_string_chain;
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 longestStrChain() {
11+
assertThat(
12+
new Solution().longestStrChain(new String[] {"a", "b", "ba", "bca", "bda", "bdca"}),
13+
equalTo(4));
14+
}
15+
16+
@Test
17+
void longestStrChain2() {
18+
assertThat(
19+
new Solution()
20+
.longestStrChain(new String[] {"xbc", "pcxbcf", "xb", "cxbc", "pcxbc"}),
21+
equalTo(5));
22+
}
23+
24+
@Test
25+
void longestStrChain3() {
26+
assertThat(new Solution().longestStrChain(new String[] {"abcd", "dbqca"}), equalTo(1));
27+
}
28+
}

0 commit comments

Comments
 (0)