forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStampingTheSequence.java
64 lines (50 loc) · 1.67 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//TC : O(M(N-M))
// SC : O(1)
class Solution {
public int[] movesToStamp(String stamp, String target) {
List<Integer> reverseIndexList = new ArrayList<>();
int len = target.length();
char[] curr = target.toCharArray();
char[] targetStr = new char[len];
Arrays.fill(targetStr, '*');
while(!Arrays.equals(curr, targetStr)){
int stampIndex = fetchStampIndex(curr, stamp);
System.out.println(stampIndex);
if(stampIndex<0){
return new int[0];
} else {
update(curr, stampIndex, stamp);
}
reverseIndexList.add(stampIndex);
}
int[] ans = new int[reverseIndexList.size()];
for(int i=0;i<reverseIndexList.size();i++){
ans[i] = reverseIndexList.get(reverseIndexList.size()-1-i);
}
return ans;
}
private int fetchStampIndex(char[] curr, String stamp){
System.out.println( new String(curr));
for(int i=0;i<=curr.length - stamp.length();i++){
int j=0;
int s = i;
boolean isNonStarChar = false;
while(j<stamp.length() && s<curr.length && (curr[s] == '*' || (curr[s] == stamp.charAt(j)))) {
if(curr[s] !='*'){
isNonStarChar = true; /// ******** , ab
}
s++;
j++;
}
if(j == stamp.length() && isNonStarChar){
return i;
}
}
return -1;
}
private void update(char[] curr, int i, String stamp){
for(int j=0;j<stamp.length();j++){
curr[j+i] = '*';
}
}
}