返回介绍

solution / 2000-2099 / 2060.Check if an Original String Exists Given Two Encoded Strings / README_EN

发布于 2024-06-17 01:03:11 字数 6000 浏览 0 评论 0 收藏 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

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文