From 00b5177a7802e7f814a538dc59da952f611a77a6 Mon Sep 17 00:00:00 2001 From: abhishekyadav13 <92470955+abhishekyadav13@users.noreply.github.com> Date: Mon, 31 Oct 2022 19:35:48 +0530 Subject: [PATCH] recursive solution --- ScrambleString.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ScrambleString.cpp diff --git a/ScrambleString.cpp b/ScrambleString.cpp new file mode 100644 index 0000000..b00e5f4 --- /dev/null +++ b/ScrambleString.cpp @@ -0,0 +1,42 @@ +/* +We can scramble a string s to get a string t using the following algorithm: + +If the length of the string is 1, stop. +If the length of the string is > 1, do the following: +Split the string into two non-empty substrings at a random index, i.e., if the string is s, divide it to x and y where s = x + y. +Randomly decide to swap the two substrings or to keep them in the same order. i.e., after this step, s may become s = x + y or s = y + x. +Apply step 1 recursively on each of the two substrings x and y. +Given two strings s1 and s2 of the same length, return true if s2 is a scrambled string of s1, otherwise, return false. + +*/ + +class Solution { +public: + bool isScramble(string s1, string s2) { + if(s1==s2) + return true; + + int len = s1.length(); + int count[26] = {0}; + for(int i=0; i