Leetcode first _ Scramble String

Source: Internet
Author: User

String. The explanation of the question stem is very complicated and confusing.

What does this question mean? The final result is to disrupt the order of letters in a string, so that you can determine whether a string can be disrupted by another string. How is this process disrupted? It is very simple. To give you a string, you must first find a point and cut it into two halves, you can swap the order of the two half to disrupt the source string, that is, the character in the two half is consistent with the relative order of all characters in the other half. For each half, you can repeat the above process.

So, how do you know where to interrupt? Poor effort. How do I know whether to perform the exchange operation after the interruption? In either case, recursion is possible. There is another question: the two strings must contain exactly the same characters. How can we determine this? The most violent way is to open two new strings, sort them, and judge whether the two new phases are not equal.

class Solution {public:    bool isScramble(string s1, string s2) {        if(s1 == "" && s2 == "")            return true;        if(s1.length() != s2.length())            return false;        if(s1 == s2)            return true;        string s11(s1), s22(s2);        sort(s11.begin(), s11.end()); sort(s22.begin(), s22.end());        if(s11 != s22)            return false;        for(int i=1;i<s1.length();i++){            if(isScramble(s1.substr(0, i), s2.substr(0, i))&&isScramble(s1.substr(i, s1.length()-i), s2.substr(i, s2.length()-i)))                return true;            else if(isScramble(s1.substr(0, i), s2.substr(s2.length()-i, i))&&isScramble(s1.substr(i, s1.length()-i), s2.substr(0, s2.length()-i)))                return true;        }        return false;    }};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.