LeetCode算法系列:87.Scramble String

题目描述:

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

Below is one possible representation of s1 = "great":

    great
   /    \
  gr    eat
 / \    /  \
g   r  e   at
           / \
          a   t

To scramble the string, we may choose any non-leaf node and swap its two children.

For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".

    rgeat
   /    \
  rg    eat
 / \    /  \
r   g  e   at
           / \
          a   t

We say that "rgeat" is a scrambled string of "great".

Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".

    rgtae
   /    \
  rg    tae
 / \    /  \
r   g  ta  e
       / \
      t   a

We say that "rgtae" is a scrambled string of "great".

Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.

Example 1:

Input: s1 = "great", s2 = "rgeat"
Output: true

Example 2:

Input: s1 = "abcde", s2 = "caebd"
Output: false

算法实现:

算法实现原理比较简单,就是如果两个字符串是题目中的这种关系,那么它们一定可以不断的执行一种操作:s1和s2可以分成两个部分,s1的某个部分和s2的某个部分满足这种关系(Scramble),s1的另一个部分和s2的另一个部分满足这种关系。而两个字符串要满足这种关系的前提是两个字符串具有完全相同的字符。具体的我们通过递归的方式实现。

class Solution {
public:
    bool isScramble(string s1, string s2) {
        if(s1.empty() || s2.empty())return false;
        if(s1.length() != s2.length())return false;
        int n = s1.length();

        return isScr(s1, s2);
    }
    bool isScr(string s1, string s2){
        // bool flag = false;
        // cout << s1 << "," << s2 << endl;
        int n = s1.length();
        if(n == 1)return s1 == s2;
        if(n == 2)return s1 == s2 || (s1[0] == s2[1] && s1[1] == s2[0]);
        unordered_map<char, int> m;
        int i = 0;
        int plus = 0;
        //分两种情况考虑
        //一是s1的根节点的两个子树没有交换,即s1的前一部分对应s2的前一部分,s1的后部分与s2的后部分对应
        //二是s1的根节点的两个子树有交换,即s1的前一段对应s2的后半段,s1的后一段和s2的前一段对应
        
        //第一种情况
        while(i < n){
            m[s1[i]] ++;
            if(m[s1[i]] == 1)plus ++;
            m[s2[i]] --;            
            if(m[s2[i]] == 0)plus --;
            // cout << i << "," << plus << endl;
            i ++;
            if(plus == 0)break;
        }
        if(i < n && plus == 0)
            if(isScr(s1.substr(0,i), s2.substr(0,i)) && isScr(s1.substr(i, n - i), s2.substr(i,n - i)))
            {
                return true;
            }
        //如果第一种情况不能成功,不意味着一定会失败,可能第二种情况会成功
        i = 0, plus = 0;
        m.clear();
        while(i < n){
            m[s1[i]] ++;
            if(m[s1[i]] == 1)plus ++;
            m[s2[n - 1 - i]] --;            
            if(m[s2[n - 1 - i]] == 0)plus --;
            // cout << i << "," << plus << endl;
            i ++;
            if(plus == 0)break;
            
        }
        // cout << i << endl;
        //两个字符串种字符不匹配
        if(i == n && plus != 0)return false;
        //虽然两个字符串字符匹配,但是其不能各自分成两个部分,并在两个字符串间匹配
        if(i == n)return false;
        return isScr(s1.substr(0,i), s2.substr(n - i)) && isScr(s1.substr(i, n - i), s2.substr(0,n - i));
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值