Check if an Original String Exists Given Two Encoded Strings - Problem

An original string, consisting of lowercase English letters, can be encoded by the following steps:

  1. Arbitrarily split it into a sequence of some number of non-empty substrings.
  2. Arbitrarily choose some elements (possibly none) of the sequence, and replace each with its length (as a numeric string).
  3. Concatenate the sequence as the encoded string.

For example, one way to encode an original string "abcdefghijklmnop" might be:

  1. Split it as a sequence: ["ab", "cdefghijklmn", "o", "p"].
  2. Choose the second and third elements to be replaced by their lengths, respectively. The sequence becomes ["ab", "12", "1", "p"].
  3. 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.

Input & Output

Example 1 — Different Interpretations
$ Input: s1 = "a5b", s2 = "a5b"
Output: true
💡 Note: Both strings are identical, so they can definitely encode the same original string (either "a5b" or "a" + 5 characters + "b")
Example 2 — Number as Length
$ Input: s1 = "ab", s2 = "a2"
Output: false
💡 Note: s1 = "ab" means original has 'a' then 'b'. s2 = "a2" could mean 'a' + 2 characters, but that would be 3 total chars vs 2 in s1
Example 3 — Complex Case
$ Input: s1 = "a5b", s2 = "abc"
Output: false
💡 Note: s1 could be "a5b" (3 chars) or "a" + 5chars + "b" (7 chars). s2 is "abc" (3 chars). Only 3-char interpretation works, but "a5b" ≠ "abc"

Constraints

  • 1 ≤ s1.length, s2.length ≤ 40
  • s1 and s2 consist of digits 1-9 and lowercase English letters
  • The number of consecutive digits in s1 and s2 does not exceed 3

Visualization

Tap to expand
Check if Original String Exists - Two Encoded Strings INPUT Encoded String s1: a 5 b "a5b" Encoded String s2: a 5 b "a5b" = Letter = Number Possible Original: "a" + 5 chars + "b" e.g., "aXXXXXb" (7 chars) ALGORITHM STEPS 1 DP with Memoization State: (i, j, diff) diff = pending chars 2 Compare Characters If diff=0: match letters 'a' == 'a' --> proceed 3 Handle Numbers '5' expands to 5 chars Update diff accordingly 4 Check Final State Both strings consumed diff = 0 means match! State Transition (0,0,0) (1,1,0) (2,2,0) --> (3,3,0) = SUCCESS FINAL RESULT Both strings can decode to: "aXXXXXb" s1: "a5b" s2: "a5b" Output: true Original string exists! s1 and s2 are identical, so they decode to same string OK Key Insight: Use 3D DP with states (i, j, diff) where diff tracks the difference in "pending" characters between strings. Numbers represent variable-length substrings. Two strings match if they can reach the end with diff=0. Time Complexity: O(n * m * D) where D is the max difference range (-999 to 999) TutorialsPoint - Check if an Original String Exists Given Two Encoded Strings | Optimal Solution
Asked in
Google 42 Facebook 38 Microsoft 25
23.5K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen