Skip to content

Commit b4af8e7

Browse files
refactor 936
1 parent 699e86c commit b4af8e7

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ _If you like this project, please leave me a star._ ★
395395
|941|[Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_941.java) | |Easy|
396396
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_938.java) | |Medium| BST, recursion, DFS
397397
|937|[Reorder Log Files](https://leetcode.com/problems/reorder-log-files/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_937.java) | |Easy|
398+
|936|[Stamping The Sequence](https://leetcode.com/problems/stamping-the-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_936.java) | |Hard| String, Greedy
398399
|935|[Knight Dialer](https://leetcode.com/problems/knight-dialer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_935.java) | |Medium|
399400
|933|[Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_933.java) | |Easy|
400401
|931|[Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_931.java) | |Medium|Dynamic Programming
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _936 {
7+
public static class Solution1 {
8+
/**
9+
* credit: https://leetcode.com/problems/stamping-the-sequence/discuss/201546/12ms-Java-Solution-Beats-100
10+
* <p>
11+
* Think reversely!
12+
* How to change target to ****?!
13+
*/
14+
public int[] movesToStamp(String stamp, String target) {
15+
List<Integer> moves = new ArrayList<>();
16+
char[] s = stamp.toCharArray();
17+
char[] t = target.toCharArray();
18+
int stars = 0;
19+
boolean[] visited = new boolean[target.length()];
20+
while (stars < target.length()) {
21+
boolean doneReplace = false;
22+
for (int i = 0; i <= target.length() - stamp.length(); i++) {
23+
if (!visited[i] && canReplace(t, i, s)) {
24+
stars = doReplace(t, i, s, stars);
25+
doneReplace = true;
26+
visited[i] = true;
27+
moves.add(i);
28+
if (stars == t.length) {
29+
break;
30+
}
31+
}
32+
}
33+
if (!doneReplace) {
34+
return new int[0];
35+
}
36+
}
37+
38+
int[] result = new int[moves.size()];
39+
for (int i = 0; i < moves.size(); i++) {
40+
result[i] = moves.get(moves.size() - i - 1);
41+
}
42+
return result;
43+
}
44+
45+
private boolean canReplace(char[] t, int i, char[] s) {
46+
for (int j = 0; j < s.length; j++) {
47+
if (t[i + j] != '*' && t[i + j] != s[j]) {
48+
return false;
49+
}
50+
}
51+
return true;
52+
}
53+
54+
private int doReplace(char[] t, int i, char[] s, int stars) {
55+
for (int j = 0; j < s.length; j++) {
56+
if (t[i + j] != '*') {
57+
t[i + j] = '*';
58+
stars++;
59+
}
60+
}
61+
return stars;
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._936;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _936Test {
10+
private static _936.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _936.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{0, 2}, solution1.movesToStamp("abc", "ababc"));
20+
}
21+
}

0 commit comments

Comments
 (0)