Skip to content

Commit 0b4ead5

Browse files
committed
Runtime: 85 ms (Top 82.20%) | Memory: 46.8 MB (Top 16.95%)
1 parent bbbdeaa commit 0b4ead5

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
// Runtime: 85 ms (Top 82.20%) | Memory: 46.8 MB (Top 16.95%)
12
var shortestPalindrome = function(s) {
23
const rev = s.split('').reverse().join('');
34
const slen = s.length;
4-
5+
56
const z = s + '$' + rev;
67
const zlen = z.length;
7-
8+
89
const lpt = new Array(zlen).fill(0);
9-
10+
1011
for(let i = 1; i < zlen; i++) {
1112
let j = lpt[i-1];
12-
13-
while(j > 0 && z.charAt(i) != z.charAt(j))
13+
14+
while(j > 0 && z.charAt(i) != z.charAt(j))
1415
j = lpt[j-1];
15-
16+
1617
if(z.charAt(i) == z.charAt(j))
1718
j++;
18-
19+
1920
lpt[i] = j;
2021
}
21-
22+
2223
return rev.slice(0, slen - lpt.at(-1)) + s;
23-
};
24+
};

0 commit comments

Comments
 (0)