Agile A
Agile A
ChatGPT said:
ChatGPT
To compare two strings and find common substrings with a length greater than 1, you can
use a dynamic programming approach. Here's how it can be done:
python
Copy code
# Initialize variables
if dp[i][j] > 1:
common_substrings.add(str1[i - dp[i][j]:i])
return sorted(common_substrings)
# Example usage
str1 = "abcdefg"
str2 = "zabcpqrsdef"
Explanation:
o dp[i][j] stores the length of the common suffix between str1[0:i] and
str2[0:j].
o If the characters at position i-1 in str1 and j-1 in str2 match, then dp[i][j] =
dp[i-1][j-1] + 1.
2. Store Substrings:
o This ensures only substrings with a length greater than 1 are stored.
3. Output:
Example Output: