Scramble String

You are given an integer ‘N’ and two strings ‘S’ and 'R' each having size = ‘N’. You can scramble the string ‘S’ to obtain string 'R' using the following operations:

1. If the length of the string is greater than 1:

  • Select any random index and split the string into two non-empty substrings. For e.g: if the string is ‘S’, then divide it into two non-empty substrings ‘A’ and ‘B’ such that ‘S’ = ‘A’ + ‘B’.
  • You can choose to swap the two substrings or keep them in the same order, i.e., after this operation string ‘S’ may become either ‘S’ = ‘A’ + ‘B’ or ‘S’ = ‘B’ + ‘A’.
  • Apply the first step recursively on each of the two strings ‘A’ and ‘B’.
  • 2. If the length of the string is equal to 1 then stop.

    Your task is to return true if 'R' is a scrambled string of ‘S’ else return false.

    Note:

    1. Both the strings are non-empty and are of the same length.
    
    2. You can apply the above operations any number of times on ‘S’.
    
    3. The operations can only be applied on the string ‘S’.
    
    4. ‘S’ and 'R' consist of lowercase letters only.
    
    Input Format:
    The first line of the input contains an integer 'T' denoting the number of test cases.
    
    The first line of each test case contains an integer ‘N’, denoting the size of the strings.
    
    The second line of each test case contains two space-separated strings ‘S’ and 'R'.
    
    Output Format:
    For each test case print a single line containing either  “true” if the string 'R' is a scrambled string of ‘S’, else “false”.
    
    Note:
    You do not need to print anything, it has already been taken care of. Just implement the given function.
    
    Constraints:
    1 <= T <= 50
    1 <= len(S) <= 30
    
    Where ‘len(S)’ represents the length of the string ‘S’.
    
    Time Limit: 1 sec
    
    AnswerBot
    1y

    The question asks to determine if string 'R' can be obtained by scrambling string 'S' using certain operations.

    • The length of both strings 'S' and 'R' should be the same.

    • The operations can be applied r...read more

    CodingNinjas
    author
    2y

    this is simple problem based on matrix multiplication concept in recursive function we can start a loop from 1 to length of strings and call again to both part to string with respective part of anothe...read more

    CodingNinjas
    author
    2y
    Recursion
    • Since there are many possible combinations in which the strings can be scrambled the only way is to use recursion with memoization to calculate the answer.
    • The main idea is to divide the strin...read more
    CodingNinjas
    author
    2y
    Dynamic Programming (Matrix Chain Multiplication)
    • Since we are working with cutting down strings and again joining them in some other order, one of the possible approaches is to use dynamic programming...read more
    Add answer anonymously...
    Wunderman Thompson Commerce Technical Associate Interview Questions
    Stay ahead in your career. Get AmbitionBox app
    qr-code
    Helping over 1 Crore job seekers every month in choosing their right fit company
    65 L+

    Reviews

    4 L+

    Interviews

    4 Cr+

    Salaries

    1 Cr+

    Users/Month

    Contribute to help millions
    Get AmbitionBox app

    Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

    Follow us
    • Youtube
    • Instagram
    • LinkedIn
    • Facebook
    • Twitter