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.
Solution 1:
public boolean isScramble(String s1, String s2) { if(s1.equals(s2)) return true; // base case if(s1.length() != s2.length()) return false; int n = s1.length(); char[] s1Array = s1.toCharArray(); char[] s2Array = s2.toCharArray(); Arrays.sort(s2Array); Arrays.sort(s1Array); for(int i = 0; i < n; i++){ if(s1Array[i] != s2Array[i]) return false; } for(int i = 1; i < n;i++){ String s1pre = s1.substring(0,i); String s1suf = s1.substring(i); String s2pre = s2.substring(0, i); String s2suf = s2.substring(i); boolean result = isScramble(s1pre, s2pre) && isScramble(s1suf, s2suf); if(!result){ s2pre = s2.substring(0, s1.length() - i); s2suf = s2.substring(s1.length() - i); result = isScramble(s1pre, s2suf) && isScramble(s1suf, s2pre); } if(result) return true; } return false; }
Solution 2:
public boolean isScramble(String s1, String s2) { if(s1.equals(s2)) return true; // base case if(!isAnagram(s1, s2)) return false; int n = s1.length(); for(int i = 1; i < n;i++){ String s1pre = s1.substring(0,i); String s1suf = s1.substring(i); String s2pre = s2.substring(0, i); String s2suf = s2.substring(i); boolean result = isScramble(s1pre, s2pre) && isScramble(s1suf, s2suf); if(!result){ s2pre = s2.substring(0, s1.length() - i); s2suf = s2.substring(s1.length() - i); result = isScramble(s1pre, s2suf) && isScramble(s1suf, s2pre); } if(result) return true; } return false; } private boolean isAnagram(String s1, String s2) { if(s1.length() != s2.length()) return false; int[] table = new int[256]; int n = s1.length(); for(int i=0; i<n; i++) { table[s1.charAt(i)]++; } for(int i=0; i<n; i++) { char c = s2.charAt(i); if(--table[c] < 0) return false; } return true; }
相关推荐
《LeetCode 101 - A LeetCode Grinding Guide (C++ Version)》是一本专为C++程序员设计的深入解析LeetCode算法问题的指南。这本书采用彩色版,以直观的方式讲解了各种数据结构和算法,旨在帮助读者磨练编程技能,...
在JavaScript中解决LeetCode题目“Scramble String”的题解主要涉及字符串操作和动态规划的技巧。Scramble String问题是一个典型的字符串变形验证问题,需要判断一个字符串是否可以通过重新排列另一个字符串的所有...
《LeetCode 101 - A LeetCode Grinding Guide (C++ Version)》是一本面向有一定C++编程基础,但缺乏刷题经验读者的教科书和工具书。作者高畅(Chang Gao)基于其在准备实习和秋招过程中对LeetCode题目的整理和刷题...
在这个“c语言-leetcode题解之0087-scramble-string.zip”压缩包中,我们可以预见到一个或多个C语言源代码文件,这些文件包含了针对LeetCode平台上编号为87的题目的解决方案。LeetCode是一个提供在线编程题目和面试...
python python_leetcode题解之087_Scramble_String
《LeetCode---C++实现》是一本专注于C++编程语言解决LeetCode在线判题平台上的算法问题的书籍。LeetCode是程序员广泛使用的平台,它提供了大量的编程题目来提升编程技能和算法理解,尤其对于准备面试的程序员来说,...
1. leetCode-126-Word-LadderII.md:这涉及到第126题,词梯问题,通常是一个字符串转换问题,目标是找到两个单词之间的最短转换序列,每次只改变一个字母。 2. leetcode-224-Basic-Calculator.md:这是第224题,基础...
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
在这个特定的示例中,我们关注的是一个名为"perform-string-shifts.c"的C语言文件,该文件包含了一个解决特定LeetCode问题的代码实现。为深入理解其内容,我们将从以下几个方面进行探讨: 1. LeetCode平台与题库:...
leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站! ⦙⦙⦙⦙⦙⦙⦙⦙ 一个很打问题的方法。 问题来缓解离线思考。 编码前的源代码。 居住和与 leetcode.com。 下载你...
leetcode-cli 注意:这个存储库是为了临时使用而分叉的。 注意:从 webbrowser 复制 cookie 并使用leetcode user -c可以临时修复不能。 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的...
这个压缩包“leetcode--python”显然包含了与LeetCode相关的Python解题代码,可能是一个开源项目,用于存储用户或社区成员解决LeetCode问题的Python实现。 **LeetCode概述** LeetCode提供了一系列的算法和数据结构...
leetcode26 algo 算法与数据结构,练习代码 语言:java 8 开发工具:vscode,安装插件Java Extension Pack vscode有智能提示,可调试,有重构支持,满足代码练习要求,相比IDEA更轻量级,普通笔记本即可流畅运行。 ...
leetcode 答案Leetcode---算法 我对 Leetcode 算法问题的回答
leetcode 接口 该项目可帮助您使用首选的 IDE 或带有命令行界面 (CLI) 的编辑器来执行 leetcode。 入门 先决条件 Windows 10、MacOS、Linux Chrome版 >=90.0.4430 安装 # Prepare your virtual environment conda ...
leetcode-machine-swift :SOS_button: 请帮助在Sources/leetcode-machine-swift/leetcode.swift设置所有 leetcode 问题。 :SOS_button: 在 swift 编码时有 xcode 总是好的。 利用自动补全、类型检查...功能可以帮助...
leetcode-cli 一个享受 leetcode 的高效 cli 工具! 非常感谢 leetcode.com,一个非常棒的网站! ⦙⦙⦙⦙⦙⦙⦙⦙ 一个很打问题的方法。 问题来缓解离线思考。 编码前的源代码。 居住和与 leetcode.com。 下载你...
leetcode-训练 算法训练。 java $: javac hello.java java $: java hello c $: gcc hello.c 如果没有错误会生成a.out 可执行文件 c $: ./a.out leetcode cli leetcode version leetcode help leetcode help user ...
在这个压缩包中,"LeetCode-String-master"很可能是包含一系列Python语言解答的代码库,对应着LeetCode上字符串专题的所有题目。 在Python中,字符串是一种基本的数据类型,用于存储和处理文本。字符串是不可变的,...
解题思路思路和LeetCode-python 503.下一个更大元素 II一致,只是这里求的是下标的距离,而不是数值倒序搜索,用到栈,栈里存储索引情况1:若栈为