jayati

Scramble String

Mar 29th, 2023
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     unordered_map<string,bool>mp;
  4.     bool f(string s1,string s2){
  5.         int n=s1.length();
  6.         if(s1.size()==1) return s1==s2;
  7.         if(s1==s2) return true;
  8.         string k=s1+s2;
  9.         if(mp.find(k)!=mp.end()) return mp[k];
  10.         for(int i=1;i<n;i++){
  11.             if((f(s1.substr(0,i),s2.substr(0,i)) && f(s1.substr(i),s2.substr(i))) or (f(s1.substr(0,i),s2.substr(n-i)) && f(s1.substr(i),s2.substr(0,n-i))))
  12.             return mp[k]=true;
  13.         }
  14.         return mp[k]=false;
  15.     }
  16.     bool isScramble(string s1, string s2) {
  17.         return f(s1,s2);
  18.     }
  19. };
Tags: dp Hard map
Advertisement
Add Comment
Please, Sign In to add comment