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

原题链接在这里:https://leetcode.com/problems/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.

题解:

Use DFS + memo. memo[i][j][diff] means s1 up to i and s2 up to j with diff count are equal.

The diff is number difference. Say s1 is "5i", s2 is "2a", the first char of s1 is 5 and first char of s2 is 2, diff is 3.

DFS state needs current i, j index, current diff, char array of s1 and s2 and memo.

When both i, j hit the end, check if diff is equal to 0.

To cut the branches, if we calculate the value before, return the memo directly.

If current char of s1 is letter, move to the next one of s1 with diff - 1.

Vice versa, if current char of s2 is letter, move to next one of s2 with diff + 1.

If current char of s1 is digit, calculate the value and move diff - val.

Vice versa for s2.

Time Complexity: exponential. 

Space: O(l1 * l2 * 2000). l1 = s1.length(). l2 = s2.length().

AC Java:

 1 class Solution {
 2     public boolean possiblyEquals(String s1, String s2) {
 3         int l1 = s1.length();
 4         int l2 = s2.length();
 5         Boolean [][][] memo = new Boolean[l1 + 1][l2 + 1][2000];
 6         return dfs(0, 0, 0, l1, l2, s1.toCharArray(), s2.toCharArray(), memo);
 7     }
 8 
 9     private boolean dfs(int i, int j, int diff, int l1, int l2, char[] s1, char[] s2, Boolean[][][] memo){
10         if(i == l1 && j == l2){
11             return diff == 0;
12         }
13 
14         if(memo[i][j][diff + 1000] != null){
15             return memo[i][j][diff + 1000];
16         }
17 
18         if(i < l1 && j < l2 && diff == 0 && s1[i] == s2[j]){
19             if(dfs(i + 1, j + 1, diff, l1, l2, s1, s2, memo)){
20                 return memo[i][j][diff + 1000] = true;
21             }
22         }
23 
24         // If s1[i] is a letter
25         if(i < l1 && Character.isLetter(s1[i]) && diff > 0 && dfs(i + 1, j, diff - 1, l1, l2, s1, s2, memo)){
26             return memo[i + 1][j][diff + 1000] = true;
27         }
28 
29         // If s2[j] is a letter
30         if(j < l2 && Character.isLetter(s2[j]) && diff < 0 && dfs(i, j + 1, diff + 1, l1, l2, s1, s2, memo)){
31             return memo[i][j + 1][diff + 1000] = true;
32         }
33 
34         for(int k = i, val = 0; k < l1 && Character.isDigit(s1[k]); k++){
35             val = val * 10 + (int)(s1[k] - '0');
36             if(dfs(k + 1, j, diff - val, l1, l2, s1, s2, memo)){
37                 return memo[k + 1][j][diff + 1000] = true;
38             }
39         }
40 
41         for(int k = j, val = 0; k < l2 && Character.isDigit(s2[k]); k++){
42             val = val * 10 + (int)(s2[k] - '0');
43             if(dfs(i, k + 1, diff + val, l1, l2, s1, s2, memo)){
44                 return memo[i][k + 1][diff + 1000] = true;
45             }
46         }
47 
48         return memo[i][j][diff + 1000] = false;
49     }
50 }

类似Wildcard Matching.

posted @ 2024-05-07 12:33  Dylan_Java_NYC  阅读(30)  评论(0编辑  收藏  举报