2023-10-27 Daily Challenge

Today I have done leetcode's October LeetCoding Challenge with cpp.

October LeetCoding Challenge 27

Description

Longest Palindromic Substring

Given a string s, return the longest palindromic substring in s.

 

Example 1:

Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.

Example 2:

Input: s = "cbbd"
Output: "bb"

 

Constraints:

  • 1 <= s.length <= 1000
  • s consist of only digits and English letters.

Solution

class Solution {
public:
  string longestPalindrome(string s) {
    int len = s.length();
    int answer = 1;
    int begin = 0;
    for(int i = 0; i < len; ++i) {
      int left = i, right = i;
      while(left > 0 && right < len && s[left-1] == s[right]) {
        left -= 1;
        right += 1;
      }
      if(right - left > answer) {
        answer = right - left;
        begin = left;
      }
      left = i, right = i;
      while(left >= 0 && right < len && s[left] == s[right]) {
        left -= 1;
        right += 1;
      }
      if(right - left - 1 > answer) {
        answer = right - left - 1;
        begin = left + 1;
      }
      
    }
    return s.substr(begin, answer);
  }
};

// Accepted
// 180/180 cases passed (34 ms)
// Your runtime beats 76.7 % of cpp submissions
// Your memory usage beats 81.58 % of cpp submissions (7.2 MB)