[LeetCode]87.Scramble String
来源:互联网 发布:淘宝网儿童连体睡衣 编辑:程序博客网 时间:2024/05/12 11:13
下面是Scramble Stringr的代码
class Solution(object): def isScramble(self, s1, s2): """ :type s1: str :type s2: str :rtype: bool """ if(s1==s2): return True if len(s1)!=len(s2): return False //Note that len(s1)-1 not len(s1) for i in range(len(s1)-1): for j in range(len(s2)-1): a,b,c,d=s1[:i+1],s1[i+1:],s2[:j+1],s2[j+1:] if(sorted(a)==sorted(c)): if(self.isScramble(a,c) and self.isScramble(b,d)): return True if(sorted(a)==sorted(d)): if(self.isScramble(a,d)and self.isScramble(b,c)): return True return False
题解:例如,”great”,”rgeat”,”rgtea”,”rgtae”都是可以匹配,那么我们如何判断两个字符串S和P是否可以匹配呢?假设S和P的长度为N,那么S和P分别可以有N-1种划分方式。比如“great”这个单词,我们可以将其划分为“g-reat”,”gr-eat”,”gre-eat”,”grea-t”,共有4种划分方式,同理要匹配的对应串也是如此。一对字符串可以匹配,那么他们两两对应的组合也应该是可匹配的。
我们假设s划分为a、b,p划分为c、d。那么要么可能是ac,bd or ad,bc。当然匹配的前提是两个字符串出现的字符是一样的,我们只需要对其进行排序再判断是否相等就行了。
理解了上面,就知道这是一个递规的过程。
0 0
- LeetCode 87. Scramble String
- [Leetcode] 87. Scramble String
- LeetCode --- 87. Scramble String
- [leetcode] 87.Scramble String
- Leetcode 87. Scramble String
- Leetcode 87. Scramble String
- Leetcode:87. Scramble String
- leetcode.87. Scramble String
- Leetcode 87. Scramble String
- [LeetCode] 87. Scramble String
- leetcode-87. Scramble String
- [LeetCode]87. Scramble String
- [LeetCode]87.Scramble String
- [LeetCode] 87. Scramble String
- 【LeetCode】87. Scramble String
- [LeetCode]87. Scramble String
- [leetcode] 87. Scramble String
- Leetcode 87. Scramble String
- 图片裁剪/旋转/缩放
- LeetCode 20. Valid Parentheses
- git设置代理服务器
- UITableView的区头显示(plain风格区头悬浮显示;grouped风格则区头跟随表格滑动)
- 信号量:整型、记录型信号量以及利用信号量实现进程互斥和前驱关系
- [LeetCode]87.Scramble String
- lucene建立索引
- java socket 传送字节流(前后台)
- css之背景图片和插入图片的区别以及精灵图的使用
- 浅谈拒绝服务攻击的原理与防御(1):普通拒绝服务攻击
- 程序员编程技术迅速提高终极攻略
- 机器人的运动范围
- Android之MVVM开发模式和DataBinding的简单用法
- Java的System.getProperty()方法获取信息的用法