From d99f83b3ca6f6e8a770ff614862c752e7e892b27 Mon Sep 17 00:00:00 2001 From: Kanishk <56789607+kanishkguptagit@users.noreply.github.com> Date: Sat, 29 Oct 2022 16:47:26 +0530 Subject: [PATCH] Create ScrambledString.java --- DynamicProgramming/ScrambledString.java | 57 +++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 DynamicProgramming/ScrambledString.java diff --git a/DynamicProgramming/ScrambledString.java b/DynamicProgramming/ScrambledString.java new file mode 100644 index 0000000..adc5cef --- /dev/null +++ b/DynamicProgramming/ScrambledString.java @@ -0,0 +1,57 @@ +import java.util.*; + +public class ScrambledString +{ + static HashMap t = new HashMap<>(); + + static boolean isScrambled(String a, String b) + { + if(a.compareTo(b) == 0) + return true; + + if(a.length() <= 1) + return false; + + int n = a.length(); + + String key = a+" "+b; + + if(t.containsKey(key)) + { + return t.get(key); + } + + boolean flag = false; + for (int i=1; i<=n-1; i++) + { + if((isScrambled(a.substring(0,i), b.substring(n-i,n)) && + isScrambled(a.substring(i,n), b.substring(0,n-i))) + || + (isScrambled(a.substring(0,i), b.substring(0,i)) && + isScrambled(a.substring(i,n), b.substring(i,n))) + ) + { + flag = true; + break; + } + } + t.put(key,flag); + return flag; + } + + public static void main(String[] args) + { + String a,b; + Scanner in = new Scanner(System.in); + a = in.next(); + b = in.next(); + + if(a.length() != b.length()) + System.out.print(false); + + if(a.length() == 0) + System.out.print(false); + + System.out.print(isScrambled(a,b)); + } +}