Lädt...

🔧 1092. Shortest Common Supersequence


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

1092. Shortest Common Supersequence

Difficulty: Hard

Topics: String, Dynamic Programming

Given two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If there are multiple valid strings, return any of them.

A string s is a subsequence of string t if deleting some number of characters from t (possibly 0) results in the string s.

Example 1:

  • Input: str1 = "abac", str2 = "cab"
  • Output: "cabac"
  • Explanation:
    • str1 = "abac" is a subsequence of "cabac" because we can delete the first "c".
    • str2 = "cab" is a subsequence of "cabac" because we can delete the last "ac".
    • The answer provided is the shortest such string that satisfies these properties.

Example 2:

  • Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"
  • Output: "aaaaaaaa"

Constraints:

  • 1 <= str1.length, str2.length <= 1000
  • str1 and str2 consist of lowercase English letters.

Hint:

  1. We can find the length of the longest common subsequence between str1[i:] and str2[j:] (for all (i, j)) by using dynamic programming.
  2. We can use this information to recover the shortest common supersequence.

Solution:

We need to find the shortest common supersequence (SCS) of two given strings. The SCS is the shortest string that contains both input strings as subsequences. The approach involves using dynamic programming (DP) to determine the longest common subsequence (LCS) and then constructing the SCS by merging the two input strings based on the LCS.

Approach

  1. Dynamic Programming (DP) Table Construction:

    • Create a DP table where dp[i][j] represents the length of the LCS of the substrings str1[0..i-1] and str2[0..j-1].
    • Fill the DP table by comparing characters of the two strings and using previously computed values to build up the solution.
  2. Backtracking to Construct SCS:

    • Use the DP table to backtrack from dp[m][n] (where m and n are the lengths of the two strings) to construct the SCS.
    • While backtracking, append characters from either str1 or str2 based on the direction of movement in the DP table. If characters match, they are part of the LCS and added only once. If they don't match, move in the direction of the larger DP value and append the corresponding character.
  3. Handling Remaining Characters:

    • After backtracking, append any remaining characters from either string that were not processed during the backtracking phase.
  4. Reverse the Result:

    • Since the characters are collected in reverse order during backtracking, reverse the result to get the correct order of the SCS.

Let's implement this solution in PHP: 1092. Shortest Common Supersequence

<?php
/**
 * @param String $str1
 * @param String $str2
 * @return String
 */
function shortestCommonSupersequence($str1, $str2) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Example Cases
echo shortestCommonSupersequence("abac", "cab") . "\n";  // Output: "cabac"
echo shortestCommonSupersequence("aaaaaaaa", "aaaaaaaa") . "\n";  // Output: "aaaaaaaa"
?>

Explanation:

  1. DP Table Construction: The DP table is built by comparing each character of the two strings. If characters match, the value is incremented from the diagonal cell. If they don't match, the value is taken from the maximum of the left or upper cell.

  2. Backtracking: Starting from the end of both strings, characters are added to the result based on their presence in the LCS. If characters match, they are added once. If not, the direction with the higher DP value is chosen, and the corresponding character is added.

  3. Handling Remaining Characters: After the main loop, any remaining characters from either string are appended to ensure all characters are included in the SCS.

  4. Reversing the Result: The collected characters are reversed to form the correct order of the SCS.

This approach efficiently constructs the shortest common supersequence by leveraging the LCS, ensuring that the solution is both optimal and correct.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

...

🔧 1092. Shortest Common Supersequence


📈 89.53 Punkte
🔧 Programmierung

💾 Red Hat Security Advisory 2024-1092-03


📈 22.24 Punkte
💾 IT Security Tools

🕵️ CB-K20/1092


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2024-1092 | Feedzy RSS Aggregator Plugin up to 4.4.1 on WordPress authorization


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ Microsoft Internet Explorer 9/11 memory corruption [CVE-2020-1092]


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2023-1092 | OAuth Single Sign On Free Plugin on WordPress cross-site request forgery


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2020-1092


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2023-1092


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ High CVE-2019-1092: Microsoft Chakracore


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ Midnight commander up to 4.5.55 denial of service [CVE-2004-1092]


📈 22.24 Punkte
🕵️ Sicherheitslücken

📰 Red Hat Security Advisory 2023-1092-01


📈 22.24 Punkte
🐧 Unix Server

🕵️ CB-K20/1092 Update 11


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CB-K20/1092 Update 10


📈 22.24 Punkte
🕵️ Sicherheitslücken

🔧 Day 1092 : Pay For It


📈 22.24 Punkte
🔧 Programmierung

🕵️ CB-K20/1092 Update 9


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CB-K20/1092 Update 8


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CB-K20/1092 Update 7


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CB-K20/1092 Update 6


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CB-K20/1092 Update 5


📈 22.24 Punkte
🕵️ Sicherheitslücken

📰 DistroWatch Weekly, Issue 1092


📈 22.24 Punkte
🐧 Unix Server

🕵️ CB-K20/1092 Update 4


📈 22.24 Punkte
🕵️ Sicherheitslücken

📰 DistroWatch Weekly, Issue 1092


📈 22.24 Punkte
🐧 Unix Server

🕵️ CB-K20/1092 Update 3


📈 22.24 Punkte
🕵️ Sicherheitslücken

🕵️ CB-K20/1092 Update 2


📈 22.24 Punkte
🕵️ Sicherheitslücken

🔧 214. Shortest Palindrome


📈 20.98 Punkte
🔧 Programmierung

🔧 Approximates a minimum Steiner tree by greedily connecting terminals with the shortest edges.


📈 20.98 Punkte
🔧 Programmierung

🕵️ Cisco IOS XR 5.1 Open Shortest Path First Version 3 memory corruption


📈 20.98 Punkte
🕵️ Sicherheitslücken

🔧 Finding Shortest Paths


📈 20.98 Punkte
🔧 Programmierung

🔧 Finds the shortest path on a 2D grid (with obstacles) using the A* search algorithm.


📈 20.98 Punkte
🔧 Programmierung

matomo