We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bbbdeaa commit 0b4ead5Copy full SHA for 0b4ead5
scripts/algorithms/S/Shortest Palindrome/Shortest Palindrome.js
@@ -1,23 +1,24 @@
1
+// Runtime: 85 ms (Top 82.20%) | Memory: 46.8 MB (Top 16.95%)
2
var shortestPalindrome = function(s) {
3
const rev = s.split('').reverse().join('');
4
const slen = s.length;
-
5
+
6
const z = s + '$' + rev;
7
const zlen = z.length;
8
9
const lpt = new Array(zlen).fill(0);
10
11
for(let i = 1; i < zlen; i++) {
12
let j = lpt[i-1];
13
- while(j > 0 && z.charAt(i) != z.charAt(j))
14
+ while(j > 0 && z.charAt(i) != z.charAt(j))
15
j = lpt[j-1];
16
17
if(z.charAt(i) == z.charAt(j))
18
j++;
19
20
lpt[i] = j;
21
}
22
23
return rev.slice(0, slen - lpt.at(-1)) + s;
-};
24
+};
0 commit comments