codeparrot/xlcost-text-to-code huggingface dataset:
Max count of N using digits of M such that 2 and 5 , and , 6 and 9 can be treated as same respectively | Java program for the above approach ; Function to find the count of numbers that can be formed using the given digits in the string ; Store the frequency of digits from the given string M ; Store length of the string M ; Loop to traverse the string ; Replace 5 with 2 ; Replace 9 with 6 ; Get the int form of the current character in the string ; Insert in the map ; Store all the digits of the required number N ; Loop to get all the digits from the number N ; Get the last digit as the remainder ; Replace 5 with 2 ; Replace 9 with 6 ; Insert the remainders in the rems map ; Store the resultant count ; Iterate through the rems map ; Get the key which is a digit from the number N to be formed ; If not present in the string M , number N that cannot be formed ; Divide the frequency of the digit from the string M with the frequency of the current remainder ; Choose the minimum ; Return the maximum count ; Driver Code
import java . util . HashMap ; import java . util . Map ; public class GFG { int solve ( int n , String str ) { HashMap < Integer , Integer > mymap = new HashMap < > ( ) ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = str . charAt ( i ) ; if ( c == '5' ) c = '2' ; else if ( c == '9' ) c = '6' ; int c_int = Integer . parseInt ( String . valueOf ( c ) ) ; if ( mymap . containsKey ( c_int ) ) mymap . put ( c_int , mymap . get ( c_int ) + 1 ) ; else mymap . put ( c_int , 1 ) ; } HashMap < Integer , Integer > rems = new HashMap < > ( ) ; while ( n > 0 ) { int rem = n % 10 ; if ( rem == 5 ) rem = 2 ; if ( rem == 9 ) rem = 6 ; if ( rems . containsKey ( rem ) ) rems . put ( rem , rems . get ( rem ) + 1 ) ; else rems . put ( rem , 1 ) ; n = n / 10 ; } int cnt = Integer . MAX_VALUE ; for ( Map . Entry < Integer , Integer > ele : rems . entrySet ( ) ) { int key = ele . getKey ( ) ; if ( ! mymap . containsKey ( key ) ) return 0 ; int temp = mymap . get ( key ) / ele . getValue ( ) ; cnt = Math . min ( cnt , temp ) ; } return cnt ; } public static void main ( String args [ ] ) { GFG obj = new GFG ( ) ; int N = 56 ; String M = "245769" ; System . out . println ( obj . solve ( N , M ) ) ; } }
Minimize swaps of pairs of characters required such that no two adjacent characters in the string are same | Java program for the above approach ; Function to check if S contains any pair of adjacent characters that are same ; Traverse the String S ; If current pair of adjacent characters are the same ; Return true ; Utility function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Check if the required String is formed already ; Traverse the String S ; Swap the characters at i and j position ; Swap for Backtracking Step ; Function to find the minimum number of swaps of pair of characters required to make all pairs of adjacent characters different ; Stores the resultant minimum number of swaps required ; Function call to find the minimum swaps required ; Print the result ; Driver Code
import java . util . * ; class GFG { static int ansSwaps ; static boolean check ( char [ ] S ) { for ( int i = 1 ; i < S . length ; i ++ ) { if ( S [ i - 1 ] == S [ i ] ) { return false ; } } return true ; } static void minimumSwaps ( char [ ] S , int swaps , int idx ) { if ( check ( S ) ) { ansSwaps = Math . min ( ansSwaps , swaps ) ; } for ( int i = idx ; i < S . length ; i ++ ) { for ( int j = i + 1 ; j < S . length ; j ++ ) { swap ( S , i , j ) ; minimumSwaps ( S , swaps + 1 , i + 1 ) ; S = swap ( S , i , j ) ; } } } static char [ ] swap ( char [ ] arr , int i , int j ) { char temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } static void findMinimumSwaps ( char [ ] S ) { ansSwaps = Integer . MAX_VALUE ; minimumSwaps ( S , 0 , 0 ) ; if ( ansSwaps == Integer . MAX_VALUE ) System . out . print ( " - 1" ) ; else System . out . print ( ansSwaps ) ; } public static void main ( String [ ] args ) { String S = " ABAACD " ; findMinimumSwaps ( S . toCharArray ( ) ) ; } }
Maximum count of 0 s between two 1 s in given range for Q queries | Java program for the above approach ; Function to count the number of 0 s lying between the two 1 s for each query ; Stores count of 0 ' s ▁ that ▁ are ▁ ▁ right ▁ to ▁ the ▁ most ▁ recent ▁ 1' s ; Stores count of 0 ' s ▁ that ▁ are ▁ ▁ left ▁ to ▁ the ▁ most ▁ recent ▁ 1' s ; Stores the count of zeros in a prefix / suffix of array ; Stores the count of total 0 s ; Traverse the string S ; If current character is '1' ; Otherwise ; Update the rightBound [ i ] ; Update count and total to 0 ; Traverse the string S in reverse manner ; If current character is '1' ; Otherwise ; Update the leftBound [ i ] ; Traverse given query array ; Update the value of count ; Print the count as the result to the current query [ L , R ] ; Driver Code
import java . lang . * ; import java . util . * ; class GFG { static void countOsBetween1s ( String S , int N , int [ ] [ ] Q ) { int [ ] leftBound = new int [ N ] ; int [ ] rightBound = new int [ N ] ; int count = 0 ; int total = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) { count = total ; } else if ( S . charAt ( i ) == '0' ) { total ++ ; } rightBound [ i ] = count ; } count = 0 ; total = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '1' ) { count = total ; } else if ( S . charAt ( i ) == '0' ) { total ++ ; } leftBound [ i ] = count ; } for ( int q = 0 ; q < Q . length ; q ++ ) { int L = Q [ q ] [ 0 ] ; int R = Q [ q ] [ 1 ] ; count = leftBound [ L ] + rightBound [ R ] - total ; System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { String S = "1001010" ; int Q [ ] [ ] = { { 0 , 4 } , { 0 , 5 } } ; int N = S . length ( ) ; countOsBetween1s ( S , N , Q ) ; } }
Get level of a node in binary tree | iterative approach | Java program to print level of given node in binary tree iterative approach ; node of binary tree ; utility function to return level of given node ; extra NULL is pushed to keep track of all the nodes to be pushed before level is incremented by 1 ; Driver Code ; create a binary tree ; return level of node
import java . io . * ; import java . util . * ; class GFG { static class node { int data ; node left , right ; node ( int data ) { this . data = data ; this . left = this . right = null ; } } static int getLevel ( node root , int data ) { Queue < node > q = new LinkedList < > ( ) ; int level = 1 ; q . add ( root ) ; q . add ( null ) ; while ( ! q . isEmpty ( ) ) { node temp = q . poll ( ) ; if ( temp == null ) { if ( q . peek ( ) != null ) { q . add ( null ) ; } level += 1 ; } else { if ( temp . data == data ) { return level ; } if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } } } return 0 ; } public static void main ( String [ ] args ) { node root = new node ( 20 ) ; root . left = new node ( 10 ) ; root . right = new node ( 30 ) ; root . left . left = new node ( 5 ) ; root . left . right = new node ( 15 ) ; root . left . right . left = new node ( 12 ) ; root . right . left = new node ( 25 ) ; root . right . right = new node ( 40 ) ; int level = getLevel ( root , 30 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 30 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 30 ▁ not ▁ found " ) ; level = getLevel ( root , 12 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 12 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 12 ▁ not ▁ found " ) ; level = getLevel ( root , 25 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 25 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 25 ▁ not ▁ found " ) ; level = getLevel ( root , 27 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 27 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 27 ▁ not ▁ found " ) ; } }
Lexicographically smallest Palindromic Path in a Binary Tree | Java program for the above approach ; Struct binary tree node ; Function to create a new node ; Function to check if the String is palindrome or not ; Function to find the lexicographically smallest palindromic path in the Binary Tree ; Base case ; Append current node 's  data to the String ; Check if a node is leaf or not ; Check for the 1 st Palindromic Path ; Store lexicographically the smallest palindromic path ; Recursively traverse left subtree ; Recursively traverse right subtree ; Function to get smallest lexographical palindromic path ; Variable which stores the final result ; Function call to compute lexicographically smallest palindromic Path ; Driver Code ; Conbinary tree
import java . util . * ; class GFG { static String finalAns = " " ; static class Node { char data ; Node left , right ; } ; static Node newNode ( char data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static boolean checkPalindrome ( String s ) { int low = 0 , high = ( int ) s . length ( ) - 1 ; while ( low < high ) { if ( s . charAt ( low ) != s . charAt ( high ) ) return false ; low ++ ; high -- ; } return true ; } static void lexicographicallySmall ( Node root , String s ) { if ( root == null ) return ; s += root . data ; if ( root . left == null && root . right == null ) { if ( checkPalindrome ( s ) ) { if ( finalAns == " $ " ) finalAns = s ; else finalAns = finalAns . compareTo ( s ) <= 0 ? finalAns : s ; } return ; } lexicographicallySmall ( root . left , s ) ; lexicographicallySmall ( root . right , s ) ; } static void getPalindromePath ( Node root ) { finalAns = " $ " ; lexicographicallySmall ( root , " " ) ; if ( finalAns == " $ " ) System . out . print ( " No ▁ Palindromic ▁ Path ▁ exists " ) ; else System . out . print ( finalAns ) ; } public static void main ( String [ ] args ) { Node root = newNode ( ' a ' ) ; root . left = newNode ( ' c ' ) ; root . left . left = newNode ( ' a ' ) ; root . left . right = newNode ( ' g ' ) ; root . right = newNode ( ' b ' ) ; root . right . left = newNode ( ' b ' ) ; root . right . right = newNode ( ' x ' ) ; root . right . left . right = newNode ( ' a ' ) ; getPalindromePath ( root ) ; } }
Find mirror of a given node in Binary tree | Java program to find the mirror Node in Binary tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; create new Node and initialize it ; recursive function to find mirror of Node ; if any of the Node is none then Node itself and decendent have no mirror , so return none , no need to further explore ! ; if left Node is target Node , then return right 's key (that is mirror) and vice   versa  ; first recur external Nodes ; if no mirror found , recur internal Nodes ; interface for mirror search ; Driver ; target Node whose mirror have to be searched
class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node n = new Node ( ) ; n . key = key ; n . left = null ; n . right = null ; return n ; } static int findMirrorRec ( int target , Node left , Node right ) { if ( left == null right == null ) return 0 ; if ( left . key == target ) return right . key ; if ( right . key == target ) return left . key ; int mirror_val = findMirrorRec ( target , left . left , right . right ) ; if ( mirror_val != 0 ) return mirror_val ; return findMirrorRec ( target , left . right , right . left ) ; } static int findMirror ( Node root , int target ) { if ( root == null ) return 0 ; if ( root . key == target ) return target ; return findMirrorRec ( target , root . left , root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . left . right = newNode ( 7 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . left = newNode ( 8 ) ; root . right . left . right = newNode ( 9 ) ; int target = root . left . left . key ; int mirror = findMirror ( root , target ) ; if ( mirror != 0 ) System . out . println ( " Mirror ▁ of ▁ Node ▁ " + target + " ▁ is ▁ Node ▁ " + mirror ) ; else System . out . println ( " Mirror ▁ of ▁ Node ▁ " + target + " ▁ is ▁ null ▁ " ) ; } }
Pair of strings having longest common prefix of maximum length in given array | Java program for the above approach ; class of Trie ; Function to insert a string into Trie ; Stores length of the string ; Traverse the string str ; If str [ i ] is not present in current path of Trie ; Create a new node of Trie ; Update root ; Function to find the maximum length of longest common prefix in Trie with str ; Stores length of str ; Stores length of longest common prefix in Trie with str ; Traverse the string str ; If str [ i ] is present in the current path of Trie ; Update len ; Update root ; Function to print the pair having maximum length of the longest common prefix ; Stores index of the string having maximum length of longest common prefix ; Stores maximum length of longest common prefix . ; Create root node of Trie ; Insert arr [ 0 ] into Trie ; Traverse the array . ; Stores maximum length of longest common prefix in Trie with arr [ i ] ; If temp is greater than len ; Update len ; Update idx ; Traverse array arr [ ] ; Stores length of arr [ i ] ; Check if maximum length of longest common prefix > M ; Traverse string arr [ i ] and arr [ j ] ; If current character of both string does not match . ; Print pairs having maximum length of the longest common prefix ; Driver Code
import java . io . * ; import java . util . * ; class TrieNode { TrieNode [ ] child = new TrieNode [ 256 ] ; TrieNode ( ) { } } class GFG { private static void insertTrie ( TrieNode root , String str ) { int M = str . length ( ) ; for ( int i = 0 ; i < M ; i ++ ) { if ( root . child [ str . charAt ( i ) ] == null ) { root . child [ str . charAt ( i ) ] = new TrieNode ( ) ; } root = root . child [ str . charAt ( i ) ] ; } } private static int findStrLen ( TrieNode root , String str ) { int M = str . length ( ) ; int len = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( root . child [ str . charAt ( i ) ] != null ) { len ++ ; root = root . child [ str . charAt ( i ) ] ; } else { return len ; } } return len ; } private static void findMaxLenPair ( List < String > arr , int N ) { int idx = - 1 ; int len = 0 ; TrieNode root = new TrieNode ( ) ; insertTrie ( root , arr . get ( 0 ) ) ; for ( int i = 1 ; i < N ; i ++ ) { int temp = findStrLen ( root , arr . get ( i ) ) ; if ( temp > len ) { len = temp ; idx = i ; } insertTrie ( root , arr . get ( i ) ) ; } for ( int i = 0 ; i < N ; i ++ ) { int M = arr . get ( i ) . length ( ) ; if ( i != idx && M >= len ) { boolean found = true ; for ( int j = 0 ; j < len ; j ++ ) { if ( arr . get ( i ) . charAt ( j ) != arr . get ( idx ) . charAt ( j ) ) { found = false ; break ; } } if ( found ) { System . out . println ( " ( " + arr . get ( i ) + " , ▁ " + arr . get ( idx ) + " ) " ) ; return ; } } } } public static void main ( String [ ] args ) { List < String > arr = Arrays . asList ( new String [ ] { " geeksforgeeks " , " geeks " , " geeksforcse " } ) ; int N = arr . size ( ) ; findMaxLenPair ( arr , N ) ; } }
Convert given string to another by minimum replacements of subsequences by its smallest character | Java program for the above approach ; Function to return the minimum number of operation ; Storing data ; Initialize both arrays ; Stores the index of character ; Filling str1array , convChar and hashmap convertMap . ; Not possible to convert ; Calculate result Initializing return values ; Iterating the character from the end ; Increment the number of operations ; Not possible to convert ; To check whether the final element has been added in set S or not . ; Check if v1 [ j ] is present in hashmap or not ; Already converted then then continue ; Not possible to convert ; Print the result ; Driver Code ; Given strings ; Function call
import java . util . * ; import java . lang . * ; class GFG { static void transformString ( String str1 , String str2 ) { int N = str1 . length ( ) ; ArrayList < ArrayList < Integer > > convChar = new ArrayList < > ( ) ; ArrayList < ArrayList < Integer > > str1array = new ArrayList < > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { convChar . add ( new ArrayList < > ( ) ) ; str1array . add ( new ArrayList < > ( ) ) ; } Map < Integer , Character > convertMap = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { str1array . get ( str1 . charAt ( i ) - ' a ' ) . add ( i ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 . charAt ( i ) < str2 . charAt ( i ) ) { System . out . println ( - 1 ) ; return ; } else if ( str1 . charAt ( i ) == str2 . charAt ( i ) ) continue ; else { convChar . get ( str2 . charAt ( i ) - ' a ' ) . add ( i ) ; convertMap . put ( i , str2 . charAt ( i ) ) ; } } int ret = 0 ; ArrayList < ArrayList < Integer > > retv = new ArrayList < > ( ) ; for ( int i = 25 ; i >= 0 ; i -- ) { ArrayList < Integer > v = convChar . get ( i ) ; if ( v . size ( ) == 0 ) continue ; ret ++ ; ArrayList < Integer > v1 = str1array . get ( i ) ; if ( v1 . size ( ) == 0 ) { System . out . println ( - 1 ) ; return ; } boolean isScompleted = false ; for ( int j = 0 ; j < v1 . size ( ) ; j ++ ) { if ( convertMap . containsKey ( v1 . get ( j ) ) ) { char a = convertMap . get ( v1 . get ( j ) ) ; if ( a > i + ' a ' ) continue ; else { v . add ( v1 . get ( j ) ) ; isScompleted = true ; retv . add ( v ) ; break ; } } else { v . add ( v1 . get ( j ) ) ; isScompleted = true ; retv . add ( v ) ; break ; } } if ( ! isScompleted ) { System . out . println ( - 1 ) ; return ; } } System . out . println ( ret ) ; } public static void main ( String [ ] args ) { String A = " abcab " ; String B = " aabab " ; transformString ( A , B ) ; } }
Find largest subtree having identical left and right subtrees | Java program to find the largest subtree having identical left and right subtree ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Sets maxSize to size of largest subtree with identical left and right . maxSize is set with size of the maximum sized subtree . It returns size of subtree rooted with current node . This size is used to keep track of maximum size . ; string to store structure of left and right subtrees ; traverse left subtree and finds its size ; traverse right subtree and finds its size ; if left and right subtrees are similar update maximum subtree if needed ( Note that left subtree may have a bigger value than right and vice versa ) ; append left subtree data ; append current node data ; append right subtree data ; function to find the largest subtree having identical left and right subtree ; Driver program to test above functions ; Let us construct the following Tree 50 / \ 10 60 / \ / \ 5 20 70 70 / \ / \ 65 80 65 80
class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static class string { String str ; } static int maxSize ; static Node maxNode ; static class pair { int first ; String second ; pair ( int a , String b ) { first = a ; second = b ; } } static pair largestSubtreeUtil ( Node root , String str ) { if ( root == null ) return new pair ( 0 , str ) ; String left = " " , right = " " ; pair ls1 = largestSubtreeUtil ( root . left , left ) ; left = ls1 . second ; int ls = ls1 . first ; pair rs1 = largestSubtreeUtil ( root . right , right ) ; right = rs1 . second ; int rs = rs1 . first ; int size = ls + rs + 1 ; if ( left . equals ( right ) ) { if ( size > maxSize ) { maxSize = size ; maxNode = root ; } } str += " | " + left + " | " ; str += " | " + root . data + " | " ; str += " | " + right + " | " ; return new pair ( size , str ) ; } static int largestSubtree ( Node node ) { maxSize = 0 ; largestSubtreeUtil ( node , " " ) ; return maxSize ; } public static void main ( String args [ ] ) { Node root = newNode ( 50 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 60 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 20 ) ; root . right . left = newNode ( 70 ) ; root . right . left . left = newNode ( 65 ) ; root . right . left . right = newNode ( 80 ) ; root . right . right = newNode ( 70 ) ; root . right . right . left = newNode ( 65 ) ; root . right . right . right = newNode ( 80 ) ; maxNode = null ; maxSize = largestSubtree ( root ) ; System . out . println ( " Largest ▁ Subtree ▁ is ▁ rooted ▁ at ▁ node ▁ " + maxNode . data + " and its size is " + maxSize); } }
Smallest number containing all possible N length permutations using digits 0 to D | Java Program to find the minimum length string consisting of all permutations of length N of D digits ; Initialize hashset to see if all the possible permutations are present in the min length string ; To keep min length string ; Generate the required string ; Iterate over all the possible character ; Append to make a new string ; If the new string is not visited ; Add in hashset ; Call the dfs function on the last d characters ; Base case ; Append '0' n - 1 times ; Call the DFS Function ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; class GeeksforGeeks { static Set < String > visited ; static StringBuilder ans ; public static void dfs ( String curr , int D ) { for ( int x = 0 ; x < D ; ++ x ) { String neighbour = curr + x ; if ( ! visited . contains ( neighbour ) ) { visited . add ( neighbour ) ; dfs ( neighbour . substring ( 1 ) , D ) ; ans . append ( x ) ; } } } public static String reqString ( int N , int D ) { if ( N == 1 && D == 1 ) return "0" ; visited = new HashSet < > ( ) ; ans = new StringBuilder ( ) ; StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < N - 1 ; ++ i ) { sb . append ( "0" ) ; } String start = sb . toString ( ) ; dfs ( start , D ) ; ans . append ( start ) ; return new String ( ans ) ; } public static void main ( String args [ ] ) { int N = 2 ; int D = 2 ; System . out . println ( reqString ( N , D ) ) ; } }
Check if a string is a scrambled form of another string | Java program to check if a given string is a scrambled form of another string ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Converting string to character array ; Checking condition for Anagram ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; If none of the above conditions are satisfied ; Driver Code
import java . util . * ; class GFG { static boolean isScramble ( String S1 , String S2 ) { if ( S1 . length ( ) != S2 . length ( ) ) { return false ; } int n = S1 . length ( ) ; if ( n == 0 ) { return true ; } if ( S1 . equals ( S2 ) ) { return true ; } char [ ] tempArray1 = S1 . toCharArray ( ) ; char [ ] tempArray2 = S2 . toCharArray ( ) ; Arrays . sort ( tempArray1 ) ; Arrays . sort ( tempArray2 ) ; String copy_S1 = new String ( tempArray1 ) ; String copy_S2 = new String ( tempArray2 ) ; if ( ! copy_S1 . equals ( copy_S2 ) ) { return false ; } for ( int i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( i , n ) , S2 . substring ( i , n ) ) ) { return true ; } if ( isScramble ( S1 . substring ( n - i , n ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( 0 , n - i ) , S2 . substring ( i , n ) ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String S1 = " coder " ; String S2 = " ocred " ; if ( isScramble ( S1 , S2 ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }