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

@problem@discussion
#String#Dynamic Programming



1/**
2 * [2060] Check if an Original String Exists Given Two Encoded Strings
3 *
4 * An original string, consisting of lowercase English letters, can be encoded by the following steps:
5 * 
6 * 	Arbitrarily split it into a sequence of some number of non-empty substrings.
7 * 	Arbitrarily choose some elements (possibly none) of the sequence, and replace each with its length (as a numeric string).
8 * 	Concatenate the sequence as the encoded string.
9 * 
10 * For example, one way to encode an original string "abcdefghijklmnop" might be:
11 * 
12 * 	Split it as a sequence: ["ab", "cdefghijklmn", "o", "p"].
13 * 	Choose the second and third elements to be replaced by their lengths, respectively. The sequence becomes ["ab", "12", "1", "p"].
14 * 	Concatenate the elements of the sequence to get the encoded string: "ab121p".
15 * 
16 * 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.
17 * Note: The test cases are generated such that the number of consecutive digits in s1 and s2 does not exceed 3.
18 *  
19 * Example 1:
20 * 
21 * Input: s1 = "internationalization", s2 = "i18n"
22 * Output: true
23 * Explanation: It is possible that "internationalization" was the original string.
24 * - "internationalization" 
25 *   -> Split:       ["internationalization"]
26 *   -> Do not replace any element
27 *   -> Concatenate:  "internationalization", which is s1.
28 * - "internationalization"
29 *   -> Split:       ["i", "nternationalizatio", "n"]
30 *   -> Replace:     ["i", "18",                 "n"]
31 *   -> Concatenate:  "i18n", which is s2
32 * 
33 * Example 2:
34 * 
35 * Input: s1 = "l123e", s2 = "44"
36 * Output: true
37 * Explanation: It is possible that "leetcode" was the original string.
38 * - "leetcode" 
39 *   -> Split:      ["l", "e", "et", "cod", "e"]
40 *   -> Replace:    ["l", "1", "2",  "3",   "e"]
41 *   -> Concatenate: "l123e", which is s1.
42 * - "leetcode" 
43 *   -> Split:      ["leet", "code"]
44 *   -> Replace:    ["4",    "4"]
45 *   -> Concatenate: "44", which is s2.
46 * 
47 * Example 3:
48 * 
49 * Input: s1 = "a5b", s2 = "c5b"
50 * Output: false
51 * Explanation: It is impossible.
52 * - The original string encoded as s1 must start with the letter 'a'.
53 * - The original string encoded as s2 must start with the letter 'c'.
54 * 
55 *  
56 * Constraints:
57 * 
58 * 	1 <= s1.length, s2.length <= 40
59 * 	s1 and s2 consist of digits 1-9 (inclusive), and lowercase English letters only.
60 * 	The number of consecutive digits in s1 and s2 does not exceed 3.
61 * 
62 */
63pub struct Solution {}
64
65// problem: https://leetcode.com/problems/check-if-an-original-string-exists-given-two-encoded-strings/
66// discuss: https://leetcode.com/problems/check-if-an-original-string-exists-given-two-encoded-strings/discuss/?currentPage=1&orderBy=most_votes&query=
67
68// submission codes start here
69
70impl Solution {
71    pub fn possibly_equals(s1: String, s2: String) -> bool {
72        false
73    }
74}
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_2060() {
84    }
85}
86


Back
© 2025 bowen.ge All Rights Reserved.