Skip to content

Commit dd9cd69

Browse files
authored
2022-08-21 update: added "Stamping The Sequence"
* 2022-08-21 update: added "Stamping The Sequence" * 2022-08-21 update: added "Stamping The Sequence"
1 parent 29e9bb9 commit dd9cd69

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Soon.
3434
| 297. Serialize and Deserialize Binary Tree | [Link](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/SerializeAndDeserializeBinaryTree.java) |
3535
| 458. Poor Pigs | [Link](https://leetcode.com/problems/poor-pigs/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/PoorPigs.java) |
3636
| 871. Minimum Number of Refueling Stops | [Link](https://leetcode.com/problems/minimum-number-of-refueling-stops/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/MinimumNumberOfRefuelingStops.java) |
37+
| 936. Stamping The Sequence | [Link](https://leetcode.com/problems/stamping-the-sequence/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/StampingTheSequence.java) |
3738
| 1106. Parsing A Boolean Expression | [Link](https://leetcode.com/problems/parsing-a-boolean-expression/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/ParsingABooleanExpression.java) |
3839
| 1220. Count Vowels Permutation | [Link](https://leetcode.com/problems/count-vowels-permutation/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/CountVowelsPermutation.java) |
3940
| 1944. Number of Visible People in a Queue | [Link](https://leetcode.com/problems/number-of-visible-people-in-a-queue/) | [Link](./src/main/java/com/smlnskgmail/jaman/leetcodejava/hard/NumberOfVisiblePeopleInAQueue.java) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.smlnskgmail.jaman.leetcodejava.hard;
2+
3+
import java.util.*;
4+
5+
// https://leetcode.com/problems/stamping-the-sequence/
6+
public class StampingTheSequence {
7+
8+
private final String stamp;
9+
private final String target;
10+
11+
public StampingTheSequence(String stamp, String target) {
12+
this.stamp = stamp;
13+
this.target = target;
14+
}
15+
16+
public int[] solution() {
17+
int m = stamp.length();
18+
int n = target.length();
19+
Queue<Integer> q = new ArrayDeque<>();
20+
boolean[] done = new boolean[n];
21+
Stack<Integer> s = new Stack<>();
22+
List<Node> nodes = new ArrayList<>();
23+
for (int i = 0; i <= n - m; i++) {
24+
var made = new HashSet<Integer>();
25+
var todo = new HashSet<Integer>();
26+
for (int j = 0; j < m; j++) {
27+
int res = i + j;
28+
if (target.charAt(res) == stamp.charAt(j)) {
29+
made.add(res);
30+
} else {
31+
todo.add(res);
32+
}
33+
}
34+
nodes.add(new Node(made, todo));
35+
if (todo.isEmpty()) {
36+
s.push(i);
37+
for (int j = i; j < i + m; j++) {
38+
if (!done[j]) {
39+
q.add(j);
40+
done[j] = true;
41+
}
42+
}
43+
}
44+
}
45+
while (!q.isEmpty()) {
46+
int i = q.poll();
47+
for (int j = Math.max(0, i - m + 1); j <= Math.min(n - m, i); j++) {
48+
var todo = nodes.get(j).todo;
49+
if (todo.contains(i)) {
50+
todo.remove(i);
51+
if (todo.isEmpty()) {
52+
s.push(j);
53+
for (int made : nodes.get(j).made) {
54+
if (!done[made]) {
55+
q.add(made);
56+
done[made] = true;
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
63+
for (boolean b : done) {
64+
if (!b) {
65+
return new int[0];
66+
}
67+
}
68+
int[] result = new int[s.size()];
69+
int pos = 0;
70+
while (!s.isEmpty()) {
71+
result[pos++] = s.pop();
72+
}
73+
return result;
74+
}
75+
76+
static class Node {
77+
78+
Set<Integer> made;
79+
Set<Integer> todo;
80+
81+
Node(Set<Integer> made, Set<Integer> todo) {
82+
this.made = made;
83+
this.todo = todo;
84+
}
85+
86+
}
87+
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.smlnskgmail.jaman.leetcodejava.hard;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertArrayEquals;
6+
7+
public class StampingTheSequenceTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertArrayEquals(
12+
new int[]{1, 0, 2},
13+
new StampingTheSequence("abc", "ababc").solution()
14+
);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)