Problem solution · C++

Longest Palindrome After Substring Concatenation II

Longest Palindrome After Substring Concatenation II: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Dynamic programming
Source
walkccc LeetCode Solutions
Length
43 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Longest Palindrome After Substring Concatenation II, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 43 lines of C++ from the credited upstream file 3504.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 4 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeLongest Palindrome After Substring Concatenation II · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  // 3503. Longest Palindrome After Substring Concatenation I  int longestPalindrome(string s, string t) {    const int m = s.length();    const int n = t.length();    vector<int> suffix = getPalindromeLengths(s, true);    vector<int> prefix = getPalindromeLengths(t, false);    int ans = max(ranges::max(suffix), ranges::max(prefix));    // dp[i][j] := the longest length of palindrome starting in s[i] and ending    // in t[j]    vector<vector<int>> dp(m, vector<int>(n));     for (int i = 0; i < m; ++i)      for (int j = n - 1; j >= 0; --j)        if (s[i] == t[j]) {          dp[i][j] = 2 + (i > 0 && j < n - 1 ? dp[i - 1][j + 1] : 0);          const int extend =              max(i + 1 < m ? suffix[i + 1] : 0, j > 0 ? prefix[j - 1] : 0);          ans = max(ans, dp[i][j] + extend);        }     return ans;  }  private:  vector<int> getPalindromeLengths(const string& s, bool isSuffix) {    const int n = s.length();    // dp[i][j] := True if s[i..j] is a palindrome    vector<vector<bool>> dp(n, vector<bool>(n));    // lengths[i] := length of longest palindrome in s[i..n - 1]    vector<int> lengths(n);    for (int i = n - 1; i >= 0; --i)      for (int j = i; j < n; ++j)        if (s[i] == s[j] && (j - i < 2 || dp[i + 1][j - 1])) {          dp[i][j] = true;          const int index = isSuffix ? i : j;          lengths[index] = max(lengths[index], j - i + 1);        }    return lengths;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗