Leetcode #2060: Check if an Original String Exists Given Two Encoded Strings
In this guide, we solve Leetcode #2060 Check if an Original String Exists Given Two Encoded Strings in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
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).
Quick Facts
- Difficulty: Hard
- Premium: No
- Tags: String, Dynamic Programming
Intuition
The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.
A carefully chosen DP state captures exactly what we need to build the final answer.
Approach
Define the DP state and recurrence, then compute states in the correct order.
Optionally compress space once the recurrence is clear.
Steps:
- Choose a DP state definition.
- Write the recurrence and base cases.
- Compute states in the correct order.
Example
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
Python Solution
from functools import lru_cache
def possiblyEquals(s1: str, s2: str) -> bool:
n1, n2 = len(s1), len(s2)
def parse_numbers(s: str, i: int):
nums = []
val = 0
for j in range(i, min(len(s), i + 3)):
if not s[j].isdigit():
break
val = val * 10 + int(s[j])
nums.append((val, j + 1))
return nums
def dfs(i: int, j: int, diff: int) -> bool:
if i == n1 and j == n2:
return diff == 0
if i < n1 and s1[i].isdigit():
for val, ni in parse_numbers(s1, i):
if dfs(ni, j, diff + val):
return True
if j < n2 and s2[j].isdigit():
for val, nj in parse_numbers(s2, j):
if dfs(i, nj, diff - val):
return True
if diff > 0:
if j < n2 and not s2[j].isdigit():
if dfs(i, j + 1, diff - 1):
return True
elif diff < 0:
if i < n1 and not s1[i].isdigit():
if dfs(i + 1, j, diff + 1):
return True
else:
if i < n1 and j < n2 and not s1[i].isdigit() and not s2[j].isdigit():
if s1[i] == s2[j] and dfs(i + 1, j + 1, 0):
return True
return False
return dfs(0, 0, 0)
Complexity
The time complexity is O(n·m) (typical). The space complexity is O(n·m) or optimized.
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.