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)


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