leetcode

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

0005.cpp (591B)


      1 class Solution {
      2     const string len(const string &s, int a, int b) {
      3         while (a >= 0 && b < s.size() && s[a] == s[b])
      4             a--, b++;
      5         return s.substr(a + 1, b - a - 1);
      6     }
      7 
      8   public:
      9     string longestPalindrome(string s) {
     10         string res = "", t;
     11         s.push_back(' ');
     12         for (int i = 0; i < s.size(); i++) {
     13             t = len(s, i, i);
     14             if (t.size() > res.size()) res = t;
     15             if (s[i] != s[i + 1]) continue;
     16             t = len(s, i, i + 1);
     17             if (t.size() > res.size()) res = t;
     18         }
     19         return res;
     20     }
     21 };