-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStampingTheSequence.java
49 lines (48 loc) · 1.56 KB
/
StampingTheSequence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*https://leetcode.com/problems/stamping-the-sequence/*/
class Solution {
public int[] movesToStamp(String stamp, String target) {
char[] T = target.toCharArray(), S = stamp.toCharArray();
ArrayList<Integer> result = new ArrayList<Integer>();
boolean[] visited = new boolean[T.length];
int i = 0, j = 0, sLen = stamp.length(), tLen = target.length(), stars = 0;
while (stars < tLen)
{
boolean stamped = false;
for (i = 0; i <= tLen-sLen; ++i)
{
if (!visited[i] && canBeStamped(S,T,i))
{
stamped = true;
stars += stampProcess(T,sLen,i);
visited[i] = true;
result.add(i);
if (stars == tLen) break;
}
}
if (!stamped) return new int[0];
}
int[] arr = new int[result.size()];
for (i = arr.length-1, j = 0; i >= 0; --i, ++j)
arr[i] = result.get(j);
return arr;
}
private boolean canBeStamped(char[] S, char[] T, int start)
{
int s = S.length;
for (int i = start; i < start+s; ++i)
if (T[i] != '*' && T[i] != S[i-start])
return false;
return true;
}
private int stampProcess(char[] T, int sLen, int start)
{
int count = 0;
for (int i = start; i < start+sLen; ++i)
if (T[i] != '*')
{
T[i] = '*';
++count;
}
return count;
}
}