Alien Artifact Synthesis

Given two strings A and B of lengths n and m respectively, your task is to choose a continuous substring C from A and a continuous substring D from B to maximize the following score:

Score = 4 x LCS(C, D) - |C| - |D|

Where LCS(C, D) is the length of the Longest Common Subsequence of C and D, and |C| and |D| are the lengths of the chosen substrings.

If the maximum possible score across all valid substrings is negative, return 0 (which corresponds to choosing empty substrings).

Example 1:

Input: n = 4, m = 5 , A = "abba" ,B = "babab"

Output: 5

Explanation: Choose

C = "abb"
D = "abab"

LCS: abb

Score:

4 * 3 - 3 - 4 = 12 - 7 = 5

Example 2:

Input: n = 8, m = 10 ,A = "bbbbabab" , B = "bbbabaaaaa"

Output: 12

Explanation : Some substrings produce LCS large enough to give similarity 12.

Now Your Turn!

Pick the correct output for the given input

Input: n = 5 , m = 5 , A = "abcde" , B = "abfde"

Output: ?

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n, m <= 5000
  • Both strings consist only of lowercase English letters.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int alienArtifact(int n, int m, string& a, string& b) {
// Write your code here
return 0;
}
};
Test Case

Input:

A
B
M
N