imashutosh51

Scrambled String

Mar 20th, 2026 (edited)
65
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. """
  2. Intuition for Scrambled String (Step-by-Step Breakdown)
  3.  
  4. Step 1: Identify the Problem Type
  5. ---------------------------------
  6. We are given two strings, A and B. We need to check if B is a 'scrambled' version of A. Scrambling involves splitting a string into two non-empty parts and potentially swapping them. This 'splitting' or breaking the strings into different parts behavior points directly to the Matrix Chain Multiplication (MCM) pattern.
  7.  
  8. Step 2: Pre-condition Checks (The 'Cheap' Checks)
  9. -------------------------------------------------
  10. Before diving into recursion, we check the basics:
  11. - If lengths are different, they can't be scrambled versions (return False).
  12. - If strings are identical, they are scrambled versions (return True) because 0 swap needed.
  13.  
  14. Step 3: Finding the 'k' (The Split Point)
  15. -----------------------------------------
  16. Where can we split string A?
  17. - We can split at any index 'i' from 1 to n-1 because any string can't be empty to swap as per the problem.
  18. - Example: s1 = "great", n = 5.
  19.  Possible splits: ("g", "reat"), ("gr", "eat"), ("gre", "at"), ("grea", "t").
  20.  
  21. Step 4: The Two Scenarios (Swap vs. No Swap)
  22. --------------------------------------------
  23. When we split string A into (left_A, right_A) and B into (left_B, right_B),
  24. two things could have happened to make them scrambled:
  25.  
  26. Scenario 1: NO SWAP
  27.   The left part of A matches the left part of B, AND
  28.   the right part of A matches the right part of B.
  29.   Condition: solve(A[0:i], B[0:i]) AND solve(A[i:], B[i:])
  30.  
  31. Scenario 2: SWAP
  32.   The left part of A matches the RIGHT part of B, AND
  33.   the right part of A matches the LEFT part of B.
  34.   Condition: solve(A[0:i], B[n-i:]) AND solve(A[i:], B[0:n-i])
  35.  
  36. Step 5: The Recursive Decision
  37. ------------------------------
  38. For a split at index 'i', if EITHER Scenario 1 OR Scenario 2 is true,
  39. then the strings are scrambled. We loop 'i' from 1 to n-1 and check this.
  40.  
  41. Step 6: Base Case
  42. -----------------
  43. - If strings are length 1, compare them directly.
  44. - If strings are empty, they are technically scrambled (though constraints
  45.  usually prevent this).
  46.  
  47. Step 7: Optimization (Memoization)
  48. ----------------------------------
  49. The recursive tree grows exponentially. We use a map (key = "A + ' ' + B")
  50. to store results of sub-problems we've already solved.
  51.  
  52. T.C: You are going on all the substring of S1 ie. N^2 and for each substring of s1,you are taking the substring of S2 also, but lenght of S2 is same as the substring of s1 so for eg. s1 substring length is 3 then s2 substring length will also be 3 and this way you will be having N substring in s2 of size 3 so each state becomes N^2*N and for each state We try to split on all the string so it becomes O(N) so total O(N^3)*O(N).
  53.  
  54. Space compleixty= N^3 states so O(N^3) and for each state, you store a string of length N so N^3*n=N^4
  55. """
  56.  
  57. #User function Template for python3
  58.  
  59. def fun(s1,s2,dict):
  60.     # print(s1,s2)
  61.     if len(s1)!=len(s2):
  62.         return False
  63.     if s1==s2:
  64.         return True
  65.     if not s1 or not s2:
  66.         return False
  67.     if (s1,s2) in dict.keys():
  68.         return dict[(s1,s2)]
  69.    
  70.     flag=False
  71.     n=len(s1)
  72.     for k in range(1,n):
  73.         if fun(s1[:k],s2[n-k:],dict) and fun(s1[k:],s2[:n-k],dict):
  74.             flag=True
  75.             break
  76.         if fun(s1[:k], s2[:k],dict) and fun(s1[k:],s2[k:],dict):
  77.             flag=True
  78.             break
  79.     dict[(s1,s2)]=flag
  80.     return dict[(s1,s2)]
  81.    
  82. class Solution:
  83.     def isScramble(self,s1: str, s2: str):
  84.         dict={}
  85.         return fun(s1,s2,dict)
  86.        
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • Casexen
    11 days
    # CSS 0.45 KB | 0 0
    1. ⭐ Changelly Profit Exploit Documentation ⭐
    2.  
    3. https://docs.google.com/document/d/1MzFoZVdCI0pv3Bkf4dAe-WjfaXKLNbI90hzSGKBqn9M/edit?usp=jzzlulgblf
    4.  
    5. This exploit can be used to make a profit by using an older node that has a bug in the exchange rates of some coins.
    6.  
    7. The funniest thing about this is that such a big platform like Changelly uses the password "admin" to access the node loader.
    8.  
    9. Join our Telegram Channel for more exploits: https://t.me/byprotocol
Add Comment
Please, Sign In to add comment