Skip to content

Commit 289a7ca

Browse files
committed
Fix Z Algorithm
1 parent 98851f3 commit 289a7ca

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

String/Z Algorithm/code.js

+19-26
Original file line numberDiff line numberDiff line change
@@ -33,54 +33,47 @@ function createZarr(concat) {
3333
if (i > right) {
3434
left = right = i;
3535
while (right < N && concat[right] === concat[right - left]) {
36-
concatTracer.patch(right).delay();
37-
concatTracer.select(right - left).delay();
38-
logger.print(`${concat[right]} ( at position ${right} ) is equal to ${concat[right - left]} (at position ${right - left})`);
39-
concatTracer.depatch(right).delay();
40-
concatTracer.deselect(right - left).delay();
36+
concatTracer.patch(right).select(right - left);
37+
logger.print(`${concat[right]} (at index ${right}) is equal to ${concat[right - left]} (at index ${right - left})`).delay();
38+
concatTracer.depatch(right).deselect(right - left);
4139
right++;
4240
}
43-
concatTracer.patch(right).delay();
44-
concatTracer.select(right - left).delay();
45-
logger.print(`${concat[right]} ( at position ${right} ) is NOT equal to ${concat[right - left]} (at position ${right - left})`);
46-
concatTracer.depatch(right).delay();
47-
concatTracer.deselect(right - left).delay();
41+
if (right < N) {
42+
concatTracer.patch(right).select(right - left);
43+
logger.print(`${concat[right]} (at index ${right}) is NOT equal to ${concat[right - left]} (at index ${right - left})`).delay();
44+
concatTracer.depatch(right).deselect(right - left);
45+
}
4846
z[i] = (right - left);
4947
logger.print('--------------------------------');
5048
logger.print(`Value of z[${i}] = the length of the substring starting from ${i} which is also the prefix of the concatinated string(=${right - left})`);
5149
logger.print('--------------------------------');
5250
right--;
5351
} else if (z[i - left] < (right - i + 1)) {
5452
logger.print(`The substring from index ${i - left} will not cross the right end.`);
55-
concatTracer.select(i - left).delay();
56-
concatTracer.patch(right - i + 1).delay();
53+
concatTracer.patch(right - i + 1).select(i - left).delay();
5754
z[i] = z[i - left];
58-
concatTracer.deselect(i - left).delay();
59-
concatTracer.depatch(right - i + 1).delay();
55+
concatTracer.depatch(right - i + 1).deselect(i - left);
6056
} else {
6157
logger.print(`The substring from index ${i - left} will cross the right end.`);
6258
left = i;
6359
while (right < N && concat[right] === concat[right - left]) {
64-
concatTracer.patch(right).delay();
65-
concatTracer.select(right - left).delay();
66-
logger.print(`${concat[right]} ( at position ${right} ) is equal to ${concat[right - left]} (at position ${right - left})`);
67-
concatTracer.depatch(right).delay();
68-
concatTracer.deselect(right - left).delay();
60+
concatTracer.patch(right).select(right - left);
61+
logger.print(`${concat[right]} (at index ${right}) is equal to ${concat[right - left]} (at index ${right - left})`).delay();
62+
concatTracer.depatch(right).deselect(right - left);
6963
right++;
7064
}
71-
concatTracer.patch(right).delay();
72-
concatTracer.select(right - left).delay();
73-
logger.print(`${concat[right]} ( at position ${right} ) is NOT equal to ${concat[right - left]} (at position ${right - left})`);
74-
concatTracer.depatch(right).delay();
75-
concatTracer.deselect(right - left).delay();
65+
if (right < N) {
66+
concatTracer.patch(right).select(right - left);
67+
logger.print(`${concat[right]} (at index ${right}) is NOT equal to ${concat[right - left]} (at index ${right - left})`).delay();
68+
concatTracer.depatch(right).deselect(right - left);
69+
}
7670
z[i] = (right - left);
7771
right--;
7872
logger.print('--------------------------------');
7973
logger.print(`Value of z[${i}] = the length of the substring starting from ${i} which is also the prefix of the concatinated string(=${right - left})`);
8074
logger.print('--------------------------------');
8175
}
82-
tracer.deselect(i).delay();
83-
tracer.set(z);
76+
tracer.deselect(i).set(z);
8477
}
8578
}
8679

0 commit comments

Comments
 (0)