LeetCode每日一题(87. Scramble String)

We can scramble a string s to get a string t using the following algorithm:

If the length of the string is 1, stop.
If the length of the string is > 1, do the following:
Split the string into two non-empty substrings at a random index, i.e., if the string is s, divide it to x and y where s = x + y.
Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x.
Apply step 1 recursively on each of the two substrings x and y.
Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false.

Example 1:

Input: s1 = “great”, s2 = “rgeat”
Output: true

Explanation: One possible scenario applied on s1 is:
“great” --> “gr/eat” // divide at random index.
“gr/eat” --> “gr/eat” // random decision is not to swap the two substrings and keep them in order.
“gr/eat” --> “g/r / e/at” // apply the same algorithm recursively on both substrings. divide at random index each of them.
“g/r / e/at” --> “r/g / e/at” // random decision was to swap the first substring and to keep the second substring in the same order.
“r/g / e/at” --> “r/g / e/ a/t” // again apply the algorithm recursively, divide “at” to “a/t”.
“r/g / e/ a/t” --> “r/g / e/ a/t” // random decision is to keep both substrings in the same order.
The algorithm stops now, and the result string is “rgeat” which is s2.
As one possible scenario led s1 to be scrambled to s2, we return true.

Example 2:

Input: s1 = “abcde”, s2 = “caebd”
Output: false

Example 3:

Input: s1 = “a”, s2 = “a”
Output: true

Constraints:

  • s1.length == s2.length
  • 1 <= s1.length <= 30
  • s1 and s2 consist of lowercase English letters.

原文解析, 大家自己看吧



use std::collections::HashMap;

impl Solution {
    fn dp(chars1: &Vec<char>, chars2: &Vec<char>, cache: &mut HashMap<String, bool>) -> bool {
        if let Some(&c) = cache.get(&chars1.iter().chain(chars2).collect::<String>()) {
            return c;
        }
        if chars1.len() != chars2.len() {
            return false;
        }
        if chars1 == chars2 {
            return true;
        }
        if chars1.len() == 1 && chars2.len() == 1 {
            return false;
        }
        for i in 0..chars1.len() - 1 {
            let swap = Solution::dp(
                &chars1[..=i].to_vec(),
                &chars2[chars2.len() - i - 1..].to_vec(),
                cache,
            ) && Solution::dp(
                &chars1[i + 1..].to_vec(),
                &chars2[..chars2.len() - i - 1].to_vec(),
                cache,
            );
            if swap {
                cache.insert(chars1.iter().chain(chars2).collect(), true);
                return true;
            }
            let keep = Solution::dp(&chars1[..=i].to_vec(), &chars2[..=i].to_vec(), cache)
                && Solution::dp(&chars1[i + 1..].to_vec(), &chars2[i + 1..].to_vec(), cache);
            if keep {
                cache.insert(chars1.iter().chain(chars2).collect(), true);
                return true;
            }
        }
        cache.insert(chars1.iter().chain(chars2).collect(), false);
        false
    }
    pub fn is_scramble(s1: String, s2: String) -> bool {
        Solution::dp(
            &s1.chars().collect(),
            &s2.chars().collect(),
            &mut HashMap::new(),
        )
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值