Skip to content

Commit bc4e39b

Browse files
committed
Update shortest-palindrome.cpp
1 parent 5ad0927 commit bc4e39b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Diff for: C++/shortest-palindrome.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ class Solution {
2020
// (Palindrome)abc
2121
// ^
2222
// Get non palindrome part of s.
23-
string non_palindrome = s.substr(prefix.back() + 1);
23+
int i = prefix.back();
24+
while (i >= s.length()) {
25+
i = prefix[i];
26+
}
27+
string non_palindrome = s.substr(i + 1);
2428
reverse(non_palindrome.begin(), non_palindrome.end());
2529
return non_palindrome + s; // cba(Palindrome)abc.
2630
}

0 commit comments

Comments
 (0)