leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0214.cpp (431B)


0 class Solution {
1 public:
2 string shortestPalindrome(const string &s) const {
3 const int n = s.size();
4 int i = 0, j = n - 1;
6 while (j >= 0) {
7 if (s[i] == s[j]) i++;
8 j--;
9 }
11 if (i == n) return s;
12 string remain = s.substr(i), rev = remain;
13 reverse(rev.begin(), rev.end());
15 return rev + shortestPalindrome(s.substr(0, i)) + remain;
16 }
17 };