The "Check if an Original String Exists Given Two Encoded Strings interview question" is a difficult "Hard" level string reconstruction task. You are given two encoded strings where some characters are replaced by digits (representing the number of characters skipped). For example, "a2b" could represent "abcde", "axxb", etc. You need to determine if there exists any single original string that both encoded strings could have originated from.
Meta asks the "Check if an Original String Exists coding problem" to test a candidate's mastery of Dynamic Programming and complex state management. It requires handling high levels of ambiguity and pruning a massive search space. It evaluates your ability to track the "difference" in lengths between two partial matchings.
This problem is solved using Dynamic Programming with Memoization (or Recursion).
dp(i, j, diff) where i is the current index in string 1, j is the index in string 2, and diff is the current length difference between the two (how many more characters string 1 has covered than string 2).diff > 0 and string 2 has a digit, parse the digit and subtract from diff.diff < 0 and string 1 has a digit, parse and add to diff.diff == 0, compare characters.diff is non-zero, "use" one character from the longer side to reduce the difference.
diff becomes 0.This is a very advanced "String interview pattern" problem. Focus on defining the state first. In many "match" problems, tracking the "difference" or "offset" between two pointers is the key to reducing the dimensionality of the DP.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Decode Ways II | Hard | Solve | |
| Valid Palindrome III | Hard | Solve | |
| Shortest Common Supersequence | Hard | Solve | |
| Minimum Insertion Steps to Make a String Palindrome | Hard | Solve | |
| Scramble String | Hard | Solve |