Leetcode 87. Scramble String 字符交换 解题报告
2016-06-27 11:37
369 查看
1 解题思想
好的我又回来了,事情告一段落,接下来应该会维持一个差不多的更新节奏了。这道题的意思就是说,对于一个字符串,如果可以通过一系列的,“确定一点后,前后子字符串交换得到新字符串”的过程变换得到一个新的字符串,那么就认为这两个Scramble 的
如果我们有S1,S2,那么假设我们选择判断如果在S1的i点上进行交换,是否可行(是否可能交换到S2),那么对于S1的[0:i]和[i:],那么我们需要在S2的对应位置截取,相同长度的判断(两种S2的[0:i]与S2的[0:i]对齐,剩余的再比,或者S2的[-i:0] (相当于从末尾开始数的) 与S1的[0:i]进行对比。 判断的方式很简单,看他们的字符相同与否(不要求书序,但是要求出现的字符的数量一致)
从S1 S2 开始,用如上的方法试探每一个可能的分割位置,如果分割通过后,那么再进行递归,计算下一部分是否满足要求,递归终止条件就是只有长度为1且的时候
2 原题
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. Subscribe to see which companies asked this question
3 AC解
public class Solution { /** * 这一步用来剪枝的,判断是否公用相同的字符,因为这个至少是要保证一致的 * 这里之所以用来排序的原因是,输入的字符串一般不会太大,如果说需要统计的话那么需要遍历长度2*n,然后遍历255个位置 * 排序的话,可以近似为2*lgn*n,然后遍历至多n个位置,各有千秋吧。。反正也不大,,就这么来了 * */ public boolean sameChars(String part1,String part2){ char[] chars1=part1.toCharArray(); char[] chars2=part2.toCharArray(); Arrays.sort(chars1); Arrays.sort(chars2); for(int i=0;i<chars1.length;i++){ if(chars1[i]!=chars2[i]) return false; } return true; } /** * 这道题其实任选一个点都可以做调转,之前以为只能二分,进坑了 * * 基本思想就是说,s1与s2,在s1上选择一个位置,分为左边s1left和右边s1right * 那么在s2上可以按照这个长度,和规则找出两种情况c1和c2,使用递归判断s1和c1或c2其中一组是否是Scramble的就可以了 * * * */ public boolean isScramble(String s1, String s2) { if(s1.equals(s2)) return true; for(int i=1;i<s1.length();i++){ String s1left=s1.substring(0,i); String s1right=s1.substring(i); //case 1 String c1left=s2.substring(0,i); String c1right=s2.substring(i); if(sameChars(s1left,c1left) && sameChars(s1right,c1right) && isScramble(s1left,c1left) && isScramble(s1right,c1right)) return true; //case 2 String c2left=s2.substring(s2.length()-i); String c2right=s2.substring(0,s2.length()-i); if(sameChars(s1left,c2left) && sameChars(s1right,c2right) && isScramble(s1left,c2left) && isScramble(s1right,c2right)) return true; } return false; } }
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- AVL树-自平衡二叉查找树(Java实现)
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- PostgreSQL ERROR: invalid escape string 解决办法
- C语言二叉树的非递归遍历实例分析
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- 使用C语言构建基本的二叉树数据结构
- 一波二叉树遍历问题的C++解答实例分享
- C#中string和StingBuilder内存中的区别实例分析
- 详解C++中实现继承string类的MyString类的步骤
- 举例讲解C语言程序中对二叉树数据结构的各种遍历方式
- C++非递归队列实现二叉树的广度优先遍历
- PHP STRING 陷阱原理说明