LeetCode 2060. Check if an Original String Exists Given Two Encoded Strings TypeScript Solution | Explanation + Code

CoderIndeed
0
2060. Check if an Original String Exists Given Two Encoded Strings

Description

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 <= 40
  • s1 and s2 consist of digits 1-9 (inclusive), and lowercase English letters only.
  • The number of consecutive digits in s1 and s2 does not exceed 3.

Solutions

Solution 1

TypeScript
function possiblyEquals(s1: string, s2: string): boolean { const n = s1.length, m = s2.length; let dp: Array<Array<Set<number>>> = Array.from({ length: n + 1 }, v => Array.from({ length: m + 1 }, w => new Set()), ); dp[0][0].add(0); for (let i = 0; i <= n; i++) { for (let j = 0; j <= m; j++) { for (let delta of dp[i][j]) { // s1为数字 let num = 0; if (delta <= 0) { for (let p = i; i < Math.min(i + 3, n); p++) { if (isDigit(s1[p])) { num = num * 10 + Number(s1[p]); dp[p + 1][j].add(delta + num); } else { break; } } } // s2为数字 num = 0; if (delta >= 0) { for (let q = j; q < Math.min(j + 3, m); q++) { if (isDigit(s2[q])) { num = num * 10 + Number(s2[q]); dp[i][q + 1].add(delta - num); } else { break; } } } // 数字匹配s1为字母 if (i < n && delta < 0 && !isDigit(s1[i])) { dp[i + 1][j].add(delta + 1); } // 数字匹配s2为字母 if (j < m && delta > 0 && !isDigit(s2[j])) { dp[i][j + 1].add(delta - 1); } // 两个字母匹配 if (i < n && j < m && delta == 0 && s1[i] == s2[j]) { dp[i + 1][j + 1].add(0); } } } } return dp[n][m].has(0); } function isDigit(char: string): boolean { return /^\d{1}$/g.test(char); }(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !