Magicsheet logo

Check if an Original String Exists Given Two Encoded Strings

Hard
100%
Updated 6/1/2025

Asked by 2 Companies

Check if an Original String Exists Given Two Encoded Strings

What is this problem about?

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.

Why is this asked in interviews?

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.

Algorithmic pattern used

This problem is solved using Dynamic Programming with Memoization (or Recursion).

  1. State: 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).
  2. Choices: At each step, you can:
    • If diff > 0 and string 2 has a digit, parse the digit and subtract from diff.
    • If diff < 0 and string 1 has a digit, parse and add to diff.
    • If both have characters at i,ji, j and diff == 0, compare characters.
    • If one has a character and diff is non-zero, "use" one character from the longer side to reduce the difference.
  3. Pruning: The difference can be quite large, so memoizing states is critical to avoid O(2N)O(2^N) complexity.

Example explanation

s1="a2",s2="1b1"s1 = "a2", s2 = "1b1"

  • String 1 starts with 'a'. String 2 starts with '1' (any char).
  • If string 2's '1' was 'a', they match. diff becomes 0.
  • String 1 then has '2' (any two chars). String 2 has 'b' then '1'.
  • If string 1's first skipped character was 'b', they match.
  • If string 1's second skipped character was string 2's last '1', they match. Result: True.

Common mistakes candidates make

  • Greedy Logic: Trying to match characters greedily doesn't work because a single digit like '123' can represent 123 characters, 12+3 characters, or 1+23 characters. You must explore all splits of multi-digit numbers.
  • State Space Explosion: Failing to use memoization, causing the recursive solution to time out.
  • Handling Multi-digit Numbers: Not correctly parsing "123" into all possible sums (1+2+3, 12+3, 1+23, 123).

Interview preparation tip

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.

Similar Questions