LeetCode 题解(19): Scramble String

2024-05-28 09:32

本文主要是介绍LeetCode 题解(19): 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 ifs2 is a scrambled string of s1.


题解:

递归解法,但是无法通过时间限制。递归时那一陀计算substr的操作一定要谨慎。准备琢磨一下动态规划做法。

class Solution {
public:bool isScramble(string s1, string s2) {if(s1.length() == 1)if(s1.compare(s2) == 0)return true;bool result = false;isScrambleRecursion(s1, s2, result);return result;}bool isScrambleRecursion(string s1, string s2, bool& result){if(s1.length() == 1)if(!s1.compare(s2)){return true;}for(int div = 0; div < s1.length()-1; div++){result = (isScrambleRecursion(s1.substr(0, div+1), s2.substr(0, div+1), result) && isScrambleRecursion(s1.substr(div+1, s1.length()-div-1), s2.substr(div+1, s2.length()-div-1), result)) ||(isScrambleRecursion(s1.substr(0, div+1), s2.substr(s2.length()-1-div, div+1), result)&& isScrambleRecursion(s1.substr(div+1, s1.length()-div-1), s2.substr(0, s2.length()-div-1), result));if(result)return true;}}
};

还是递归,进行了一下“剪枝”,大大减少了不必要的比较次数,16ms通过。

class Solution {
public:bool isScramble(string s1, string s2) {if(s1.length() == 1)if(s1.compare(s2) == 0)return true;bool result = false;isScrambleRecursion(s1, s2, result);return result;}bool isScrambleRecursion(string s1, string s2, bool& result){if(s1.length() == 1)if(!s1.compare(s2)){return true;}int A[26] = {0};  for(int i = 0; i < s1.length(); i++)  ++A[s1[i]-'a'];  for(int j = 0; j < s2.length(); j++)  --A[s2[j]-'a'];  for(int k = 0; k < 26; k++)  if(A[k] != 0) return false;for(int div = 0; div < s1.length()-1; div++){result = (isScrambleRecursion(s1.substr(0, div+1), s2.substr(0, div+1), result) && isScrambleRecursion(s1.substr(div+1, s1.length()-div-1), s2.substr(div+1, s2.length()-div-1), result)) ||(isScrambleRecursion(s1.substr(0, div+1), s2.substr(s2.length()-1-div, div+1), result)&& isScrambleRecursion(s1.substr(div+1, s1.length()-div-1), s2.substr(0, s2.length()-div-1), result));if(result)return true;}}
};


这篇关于LeetCode 题解(19): Scramble String的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010186

相关文章

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

IDEA如何将String类型转json格式

《IDEA如何将String类型转json格式》在Java中,字符串字面量中的转义字符会被自动转换,但通过网络获取的字符串可能不会自动转换,为了解决IDEA无法识别JSON字符串的问题,可以在本地对字... 目录问题描述问题原因解决方案总结问题描述最近做项目需要使用Ai生成json,可生成String类型

详解Spring Boot接收参数的19种方式

《详解SpringBoot接收参数的19种方式》SpringBoot提供了多种注解来接收不同类型的参数,本文给大家介绍SpringBoot接收参数的19种方式,感兴趣的朋友跟随小编一起看看吧... 目录SpringBoot接受参数相关@PathVariable注解@RequestHeader注解@Reque

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param