Check if an Original String Exists Given Two Encoded Strings Problem
Description
LeetCode Problem 2060.
An original string, consisting of lowercase English letters, can be encoded by the following steps:
- Arbitrarily split it into a sequence of some number of non-empty substrings.
- Arbitrarily choose some elements (possibly none) of the sequence, and replace each with its length (as a numeric string).
- Concatenate the sequence as the encoded string.
For example, one way to encode an original string "abcdefghijklmnop" might be:
- Split it as a sequence:
["ab", "cdefghijklmn", "o", "p"]. - Choose the second and third elements to be replaced by their lengths, respectively. The sequence becomes
["ab", "12", "1", "p"]. - Concatenate the elements of the sequence to get the encoded string:
"ab121p".
Given two encoded strings s1 and s2, consisting of lowercase English letters and digits 1-9 (inclusive), return true if there exists an original string that could be encoded as both s1 and s2. Otherwise, return false.
Note: The test cases are generated such that the number of consecutive digits in s1 and s2 does not exceed 3.
Example 1:
Input: s1 = "internationalization", s2 = "i18n"
Output: true
Explanation: It is possible that "internationalization" was the original string.
- "internationalization"
-> Split: ["internationalization"]
-> Do not replace any element
-> Concatenate: "internationalization", which is s1.
- "internationalization"
-> Split: ["i", "nternationalizatio", "n"]
-> Replace: ["i", "18", "n"]
-> Concatenate: "i18n", which is s2
Example 2:
Input: s1 = "l123e", s2 = "44"
Output: true
Explanation: It is possible that "leetcode" was the original string.
- "leetcode"
-> Split: ["l", "e", "et", "cod", "e"]
-> Replace: ["l", "1", "2", "3", "e"]
-> Concatenate: "l123e", which is s1.
- "leetcode"
-> Split: ["leet", "code"]
-> Replace: ["4", "4"]
-> Concatenate: "44", which is s2.
Example 3:
Input: s1 = "a5b", s2 = "c5b"
Output: false
Explanation: It is impossible.
- The original string encoded as s1 must start with the letter 'a'.
- The original string encoded as s2 must start with the letter 'c'.
Constraints:
1 <= s1.length, s2.length <= 40s1ands2consist of digits1-9(inclusive), and lowercase English letters only.- The number of consecutive digits in
s1ands2does not exceed3.
Solution
Approach: DP over (i, j, difference)
Walk through both encoded strings at the same time. After reading i characters of s1 and j characters of s2, the two prefixes expand to original prefixes of certain lengths; let d be the length of the s1 prefix minus the length of the s2 prefix. Letters count as length 1, and a digit run stands for a block whose length is the number it spells. Since a run has at most three digits, d stays within about +-1000.
Let dp[i][j] be the set of reachable differences. From (i, j, d):
- If
d <= 0(s1is not ahead), read up to three consecutive digits ofs1starting ati, giving numbersnum(every prefix of the run counts), and go to(p + 1, j, d + num). - If
d >= 0, do the same ons2, going to(i, q + 1, d - num). - If
d < 0ands1[i]is a letter, it fills one position of a block froms2: go to(i + 1, j, d + 1). Symmetrically, ifd > 0ands2[j]is a letter, go to(i, j + 1, d - 1). - If
d == 0and both next characters are equal letters, go to(i + 1, j + 1, 0).
The answer is whether 0 is in dp[n][m].
Complexity: time O(n * m * D) and space O(n * m * D), where D (about 2000) bounds the number of distinct differences.
class Solution:
def possiblyEquals(self, s1: str, s2: str) -> bool:
n, m = len(s1), len(s2)
dp = [[set() for _ in range(m + 1)] for _ in range(n + 1)]
dp[0][0].add(0)
for i in range(n + 1):
for j in range(m + 1):
for d in dp[i][j]:
if d <= 0:
num = 0
for p in range(i, min(i + 3, n)):
if not s1[p].isdigit():
break
num = num * 10 + int(s1[p])
dp[p + 1][j].add(d + num)
if d >= 0:
num = 0
for q in range(j, min(j + 3, m)):
if not s2[q].isdigit():
break
num = num * 10 + int(s2[q])
dp[i][q + 1].add(d - num)
if i < n and d < 0 and not s1[i].isdigit():
dp[i + 1][j].add(d + 1)
if j < m and d > 0 and not s2[j].isdigit():
dp[i][j + 1].add(d - 1)
if i < n and j < m and d == 0 and s1[i] == s2[j]:
dp[i + 1][j + 1].add(0)
return 0 in dp[n][m]