We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5ad0927 commit bc4e39bCopy full SHA for bc4e39b
C++/shortest-palindrome.cpp
@@ -20,7 +20,11 @@ class Solution {
20
// (Palindrome)abc
21
// ^
22
// Get non palindrome part of s.
23
- string non_palindrome = s.substr(prefix.back() + 1);
+ int i = prefix.back();
24
+ while (i >= s.length()) {
25
+ i = prefix[i];
26
+ }
27
+ string non_palindrome = s.substr(i + 1);
28
reverse(non_palindrome.begin(), non_palindrome.end());
29
return non_palindrome + s; // cba(Palindrome)abc.
30
}
0 commit comments