codeparrot/xlcost-text-to-code huggingface dataset:
Count of number of given string in 2D character array | Java code for finding count of string in a given 2D character array . ; Utility function to search complete string from any given index of 2d char array ; Through Backtrack searching in every directions ; Function to search the string in 2d array ; Driver code
import java . util . * ; class GFG { static int internalSearch ( String needle , int row , int col , String hay [ ] , int row_max , int col_max , int xx ) { int found = 0 ; if ( row >= 0 && row <= row_max && col >= 0 && col <= col_max && xx < needle . length ( ) && needle . charAt ( xx ) == hay [ row ] . charAt ( col ) ) { char match = needle . charAt ( xx ) ; xx += 1 ; hay [ row ] = hay [ row ] . substring ( 0 , col ) + "0" + hay [ row ] . substring ( col + 1 ) ; if ( xx == needle . length ( ) ) { found = 1 ; } else { found += internalSearch ( needle , row , col + 1 , hay , row_max , col_max , xx ) ; found += internalSearch ( needle , row , col - 1 , hay , row_max , col_max , xx ) ; found += internalSearch ( needle , row + 1 , col , hay , row_max , col_max , xx ) ; found += internalSearch ( needle , row - 1 , col , hay , row_max , col_max , xx ) ; } hay [ row ] = hay [ row ] . substring ( 0 , col ) + match + hay [ row ] . substring ( col + 1 ) ; } return found ; } static int searchString ( String needle , int row , int col , String str [ ] , int row_count , int col_count ) { int found = 0 ; int r , c ; for ( r = 0 ; r < row_count ; ++ r ) { for ( c = 0 ; c < col_count ; ++ c ) { found += internalSearch ( needle , r , c , str , row_count - 1 , col_count - 1 , 0 ) ; } } return found ; } public static void main ( String args [ ] ) { String needle = " MAGIC " ; String input [ ] = { " BBABBM " , " CBMBBA " , " IBABBG " , " GOZBBI " , " ABBBBC " , " MCIGAM " } ; String str [ ] = new String [ input . length ] ; int i ; for ( i = 0 ; i < input . length ; ++ i ) { str [ i ] = input [ i ] ; } System . out . println ( " count : ▁ " + searchString ( needle , 0 , 0 , str , str . length , str [ 0 ] . length ( ) ) ) ; } }
Find minimum shift for longest common prefix | Java program to find longest common prefix after rotation of second string . ; function for KMP search ; p [ 1 ] = 0 ; ; find out the longest prefix and position ; for new position with longer prefix in str2 update pos and len ; print result ; Driver code
class GFG { static void KMP ( int m , int n , String str2 , String str1 ) { int pos = 0 , len = 0 ; int [ ] p = new int [ m + 1 ] ; int k = 0 ; char [ ] ch1 = str1 . toCharArray ( ) ; char [ ] ch2 = str2 . toCharArray ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { while ( k > 0 && ch1 [ k ] != ch1 [ i - 1 ] ) k = p [ k ] ; if ( ch1 [ k ] == ch1 [ i - 1 ] ) ++ k ; p [ i ] = k ; } for ( int j = 0 , i = 0 ; i < m ; i ++ ) { while ( j > 0 && j < n && ch1 [ j ] != ch2 [ i ] ) j = p [ j ] ; if ( j < n && ch1 [ j ] == ch2 [ i ] ) j ++ ; if ( j > len ) { len = j ; pos = i - j + 1 ; } } System . out . println ( " Shift ▁ = ▁ " + pos ) ; System . out . println ( " Prefix ▁ = ▁ " + str1 . substring ( 0 , len ) ) ; } public static void main ( String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " forgeeksgeeks " ; int n = str1 . length ( ) ; str2 = str2 + str2 ; KMP ( 2 * n , n , str2 , str1 ) ; } }
Find all the patterns of "1(0 + ) 1" in a given string | SET 1 ( General Approach ) | Java Code to count 1 ( 0 + ) 1 patterns in a string ; Function to count patterns ; Variable to store the last character ; We found 0 and last character was '1' , state change ; After the stream of 0 ' s , ▁ we ▁ ▁ got ▁ a ▁ ' 1 ',counter incremented ; Last character stored ; Driver Code
import java . io . * ; class GFG { static int patternCount ( String str ) { char last = str . charAt ( 0 ) ; int i = 1 , counter = 0 ; while ( i < str . length ( ) ) { if ( str . charAt ( i ) == '0' && last == '1' ) { while ( str . charAt ( i ) == '0' ) i ++ ; if ( str . charAt ( i ) == '1' ) counter ++ ; } last = str . charAt ( i ) ; i ++ ; } return counter ; } public static void main ( String [ ] args ) { String str = "1001ab010abc01001" ; System . out . println ( patternCount ( str ) ) ; } }
Boyer Moore Algorithm | Good Suffix heuristic | Java program for Boyer Moore Algorithm with Good Suffix heuristic to find pattern in given text string ; preprocessing for strong good suffix rule ; m is the length of pattern ; if character at position i - 1 is not equivalent to character at j - 1 , then continue searching to right of the pattern for border ; the character preceding the occurrence of t in pattern P is different than the mismatching character in P , we stop skipping the occurrences and shift the pattern from i to j ; Update the position of next border ; p [ i - 1 ] matched with p [ j - 1 ] , border is found . store the beginning position of border ; Preprocessing for case 2 ; set the border position of the first character of the pattern to all indices in array shift having shift [ i ] = 0 ; suffix becomes shorter than bpos [ 0 ] , use the position of next widest border as value of j ; Search for a pattern in given text using Boyer Moore algorithm with Good suffix rule ; s is shift of the pattern with respect to text ; initialize all occurrence of shift to 0 ; do preprocessing ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at the current shift , then index j will become - 1 after the above loop ; pat [ i ] != pat [ s + j ] so shift the pattern shift [ j + 1 ] times ; Driver Code
class GFG { static void preprocess_strong_suffix ( int [ ] shift , int [ ] bpos , char [ ] pat , int m ) { int i = m , j = m + 1 ; bpos [ i ] = j ; while ( i > 0 ) { while ( j <= m && pat [ i - 1 ] != pat [ j - 1 ] ) { if ( shift [ j ] == 0 ) shift [ j ] = j - i ; j = bpos [ j ] ; } i -- ; j -- ; bpos [ i ] = j ; } } static void preprocess_case2 ( int [ ] shift , int [ ] bpos , char [ ] pat , int m ) { int i , j ; j = bpos [ 0 ] ; for ( i = 0 ; i <= m ; i ++ ) { if ( shift [ i ] == 0 ) shift [ i ] = j ; if ( i == j ) j = bpos [ j ] ; } } static void search ( char [ ] text , char [ ] pat ) { int s = 0 , j ; int m = pat . length ; int n = text . length ; int [ ] bpos = new int [ m + 1 ] ; int [ ] shift = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) shift [ i ] = 0 ; preprocess_strong_suffix ( shift , bpos , pat , m ) ; preprocess_case2 ( shift , bpos , pat , m ) ; while ( s <= n - m ) { j = m - 1 ; while ( j >= 0 && pat [ j ] == text [ s + j ] ) j -- ; if ( j < 0 ) { System . out . printf ( "pattern occurs at shift = %dNEW_LINE", s); s += shift [ 0 ] ; } else s += shift [ j + 1 ] ; } } public static void main ( String [ ] args ) { char [ ] text = " ABAAAABAACD " . toCharArray ( ) ; char [ ] pat = " ABA " . toCharArray ( ) ; search ( text , pat ) ; } }
Maximum length prefix of one string that occurs as subsequence in another | Java program to find maximum length prefix of one string occur as subsequence in another string . ; Return the maximum length prefix which is subsequence . ; Iterating string T . ; If end of string S . ; If character match , increment counter . ; Driver Code
public class GFG { static int maxPrefix ( String s , String t ) { int count = 0 ; for ( int i = 0 ; i < t . length ( ) ; i ++ ) { if ( count == s . length ( ) ) break ; if ( t . charAt ( i ) == s . charAt ( count ) ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String S = " digger " ; String T = " biggerdiagram " ; System . out . println ( maxPrefix ( S , T ) ) ; } }
Replace all occurrences of string AB with C without using extra space | Java program to replace all occurrences of " AB " with " C " ; Start traversing from second character ; If previous character is ' A ' and current character is 'B" ; Replace previous character with ' C ' and move all subsequent characters one position back ; Driver code
import java . io . * ; class GFG { static void translate ( char str [ ] ) { for ( int i = 1 ; i < str . length ; i ++ ) { if ( str [ i - 1 ] == ' A ' && str [ i ] == ' B ' ) { str [ i - 1 ] = ' C ' ; int j ; for ( j = i ; j < str . length - 1 ; j ++ ) str [ j ] = str [ j + 1 ] ; str [ j ] = ' ▁ ' ; } } return ; } public static void main ( String args [ ] ) { String st = " helloABworldABGfG " ; char str [ ] = st . toCharArray ( ) ; translate ( str ) ; System . out . println ( " The ▁ modified ▁ string ▁ is ▁ : " ) ; System . out . println ( str ) ; } }
Find all occurrences of a given word in a matrix | Java Program to find all occurrences of the word in a matrix ; check whether given cell ( row , col ) is a valid cell or not . ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 8 neighboursof a given cell ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; return if current character doesn 't match with  the next character in the word ; append current character position to path ; current character matches with the last character in the word ; Recur for all connected neighbours ; The main function to find all occurrences of the word in a matrix ; traverse through the all cells of given matrix ; occurrence of first character in matrix ; check and print if path exists ; Driver code
import java . util . * ; class GFG { static final int ROW = 3 ; static final int COL = 5 ; static boolean isvalid ( int row , int col , int prevRow , int prevCol ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ! ( row == prevRow && col == prevCol ) ; } static int rowNum [ ] = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; static int colNum [ ] = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; static void DFS ( char mat [ ] [ ] , int row , int col , int prevRow , int prevCol , char [ ] word , String path , int index , int n ) { if ( index > n mat [ row ] [ col ] != word [ index ] ) return ; path += ( word [ index ] ) + " ( " + String . valueOf ( row ) + " , ▁ " + String . valueOf ( col ) + " ) ▁ " ; if ( index == n ) { System . out . print ( path + "NEW_LINE"); return ; } for ( int k = 0 ; k < 8 ; ++ k ) if ( isvalid ( row + rowNum [ k ] , col + colNum [ k ] , prevRow , prevCol ) ) DFS ( mat , row + rowNum [ k ] , col + colNum [ k ] , row , col , word , path , index + 1 , n ) ; } static void findWords ( char mat [ ] [ ] , char [ ] word , int n ) { for ( int i = 0 ; i < ROW ; ++ i ) for ( int j = 0 ; j < COL ; ++ j ) if ( mat [ i ] [ j ] == word [ 0 ] ) DFS ( mat , i , j , - 1 , - 1 , word , " " , 0 , n ) ; } public static void main ( String [ ] args ) { char mat [ ] [ ] = { { ' B ' , ' N ' , ' E ' , ' Y ' , ' S ' } , { ' H ' , ' E ' , ' D ' , ' E ' , ' S ' } , { ' S ' , ' G ' , ' N ' , ' D ' , ' E ' } } ; char [ ] word = " DES " . toCharArray ( ) ; findWords ( mat , word , word . length - 1 ) ; } }
Maximum length of string formed by concatenation having even frequency of each character | Java Implementation of the above approach ; Function to check the string ; Count the frequency of the string ; Check the frequency of the string ; Store the length of the new String ; Function to find the longest concatenated string having every character of even frequency ; Checking the string ; Dont Include the string ; Include the string ; Driver code ; Call the function ; Print the answer
import java . io . * ; import java . util . * ; public class index { static int max = 0 ; static String ans1 = " " ; static void calculate ( String ans ) { int dp [ ] = new int [ 26 ] ; for ( int i = 0 ; i < ans . length ( ) ; ++ i ) { dp [ ans . charAt ( i ) - ' A ' ] ++ ; } for ( int i = 0 ; i < dp . length ; ++ i ) { if ( dp [ i ] % 2 == 1 ) { return ; } } if ( max < ans . length ( ) ) { max = ans . length ( ) ; ans1 = ans ; } } static void longestString ( List < String > arr , int index , String str ) { if ( index == arr . size ( ) ) { return ; } longestString ( arr , index + 1 , str ) ; str += arr . get ( index ) ; calculate ( str ) ; longestString ( arr , index + 1 , str ) ; } public static void main ( String [ ] args ) { ArrayList < String > A = new ArrayList < > ( ) ; A . add ( " ABAB " ) ; A . add ( " ABF " ) ; A . add ( " CDA " ) ; A . add ( " AD " ) ; A . add ( " CCC " ) ; longestString ( A , 0 , " " ) ; System . out . println ( ans1 + " ▁ " + ans1 . length ( ) ) ; } }
Find the next number by adding natural numbers in order on alternating indices from last | Java program for the above approach ; Function to generate the resultant number using the given criteria ; Storing end result ; Find the length of numeric string ; Traverse the string ; Storing digit at ith position ; Checking that the number would be added or not ; Logic for summing the digits if the digit is greater than 10 ; Storing the result ; Returning the result ; Driver Code
class GFG { public static String generateNumber ( String number ) { int temp = 0 , adding_number = 0 ; String result = " " ; int len = number . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { int digit = ( int ) number . charAt ( i ) - ( int ) '0' ; if ( temp % 2 == 0 ) { adding_number += 1 ; digit += adding_number ; if ( digit >= 10 ) { digit %= 9 ; if ( digit == 0 ) digit = 9 ; } } result = digit + result ; temp += 1 ; } return result ; } public static void main ( String args [ ] ) { String S = "1345" ; System . out . println ( generateNumber ( S ) ) ; } }
Minimum value of K such that each substring of size K has the given character | Javva program for the above approach ; Function to find the minimum value of K such that char c occurs in all K sized substrings of string S ; Store the string length ; Store difference of lengths of segments of every two consecutive occurrences of c ; Stores the maximum difference ; Store the previous occurence of char c ; Check if the current character is c or not ; Stores the difference of consecutive occurrences of c ; Update previous occurrence of c with current occurence ; Comparing diff with max ; If string doesn 't contain c ; Return max ; Driver Code
class GFG { public static int findK ( String s , char c ) { int n = s . length ( ) ; int diff ; int max = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == c ) { diff = i - prev ; prev = i ; if ( diff > max ) { max = diff ; } } } if ( max == 0 ) return - 1 ; return max ; } public static void main ( String args [ ] ) { String S = " abdegb " ; char ch = ' b ' ; System . out . println ( findK ( S , ch ) ) ; } }
Count subsequences 01 in string generated by concatenation of given numeric string K times | Java program for the above approach ; Function to calculate the number of subsequences of "01" ; Store count of 0 ' s ▁ and ▁ 1' s ; Count of subsequences without concatenation ; Case 1 ; Case 2 ; Return the total count ; Driver Code
import java . io . * ; class GFG { static int countSubsequence ( String S , int N , int K ) { int C = 0 , C1 = 0 , C0 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) C1 ++ ; else if ( S . charAt ( i ) == '0' ) C0 ++ ; } int B1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) B1 ++ ; else if ( S . charAt ( i ) == '0' ) C = C + ( C1 - B1 ) ; } int ans = C * K ; ans += ( C1 * C0 * ( ( ( K ) * ( K - 1 ) ) / 2 ) ) ; return ans ; } public static void main ( String [ ] args ) { String S = "230013110087" ; int K = 2 ; int N = S . length ( ) ; System . out . println ( countSubsequence ( S , N , K ) ) ; } }
Minimum adjacent swaps required to make a binary string alternating | Java program for the above approach ; Function to find the minimum number of adjacent swaps to make the String alternating ; Count the no of zeros and ones ; Base Case ; Store no of min swaps when String starts with "1" ; Keep track of the odd positions ; Checking for when the String starts with "1" ; Adding the no of swaps to fix "1" at odd positions ; Store no of min swaps when String starts with "0" ; Keep track of the odd positions ; Checking for when the String starts with "0" ; Adding the no of swaps to fix "1" at odd positions ; Returning the answer based on the conditions when String length is even ; When String length is odd ; When no of ones is greater than no of zeros ; When no of ones is greater than no of zeros ; Driver Code
import java . util . * ; class GFG { static int minSwaps ( String s ) { int ones = 0 , zeros = 0 ; int N = s . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '1' ) ones ++ ; else zeros ++ ; } if ( ( N % 2 == 0 && ones != zeros ) || ( N % 2 == 1 && Math . abs ( ones - zeros ) != 1 ) ) { return - 1 ; } int ans_1 = 0 ; int j = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '1' ) { ans_1 += Math . abs ( j - i ) ; j += 2 ; } } int ans_0 = 0 ; int k = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '0' ) { ans_0 += Math . abs ( k - i ) ; k += 2 ; } } if ( N % 2 == 0 ) return Math . min ( ans_1 , ans_0 ) ; else { if ( ones > zeros ) return ans_1 ; else return ans_0 ; } } public static void main ( String [ ] args ) { String S = "110100" ; System . out . print ( minSwaps ( S ) ) ; } }
Print all ways to reach the Nth stair with the jump of 1 or 2 units at a time | Java program for the above approach ; Function to find all the ways to reach Nth stair using one or two jumps ; Base Cases ; Recur for jump1 and jump2 ; Stores the total possible jumps ; Add "1" with every element present in jump1 ; Add "2" with every element present in jump2 ; Driver Code
import java . util . * ; class GFG { static ArrayList < String > TotalPossibleJumps ( int N ) { if ( ( N - 1 ) == 0 ) { ArrayList < String > newvec = new ArrayList < String > ( ) ; newvec . add ( " " ) ; return newvec ; } else { if ( N < 0 ) { ArrayList < String > newvec = new ArrayList < String > ( ) ; return newvec ; } } ArrayList < String > jump1 = TotalPossibleJumps ( N - 1 ) ; ArrayList < String > jump2 = TotalPossibleJumps ( N - 2 ) ; ArrayList < String > totaljumps = new ArrayList < String > ( ) ; for ( String s : jump1 ) { totaljumps . add ( "1" + s ) ; } for ( String s : jump2 ) { totaljumps . add ( "2" + s ) ; } return totaljumps ; } public static void main ( String [ ] args ) { int N = 3 ; ArrayList < String > Ans = TotalPossibleJumps ( N ) ; for ( String it : Ans ) System . out . println ( it ) ; } }
Find any permutation of Binary String of given size not present in Array | Java Program for the above approach ; Function to find a binary string of N bits that does not occur in the givrn array arr [ ] ; Stores the resultant string ; Loop to iterate over all the given strings in a diagonal order ; Append the complement of element at current index into ans ; Return Answer ; Driver code
import java . io . * ; class GFG { static String findString ( String arr [ ] , int N ) { String ans = " " ; for ( int i = 0 ; i < N ; i ++ ) { ans += arr [ i ] . charAt ( i ) == '0' ? '1' : '0' ; } return ans ; } public static void main ( String [ ] args ) { String arr [ ] = { "111" , "011" , "001" } ; int N = arr . length ; System . out . println ( findString ( arr , N ) ) ; } }
Check if the string has a reversible equal substring at the ends | Java program for the above approach ; Function to print longest substring that appears at beginning of string and also at end in reverse order ; Stores the resultant string ; If the characters are same ; Otherwise , break ; If the string can 't be formed ; Otherwise print resultant string ; Driver Code
public class GFG { static void commonSubstring ( String s ) { int n = s . length ( ) ; int i = 0 ; int j = n - 1 ; String ans = " " ; while ( j >= 0 ) { if ( s . charAt ( i ) == s . charAt ( j ) ) { ans += s . charAt ( i ) ; i ++ ; j -- ; } else { break ; } } if ( ans . length ( ) == 0 ) System . out . println ( " False " ) ; else { System . out . println ( " True ▁ " ) ; System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { String S = " abca " ; commonSubstring ( S ) ; } }
Length of Smallest Non Prime Subsequence in given numeric String | Java program for the above approach ; Function to find the smallest length of resultant subsequence ; Check for a subsequence of size 1 ; Check for a subsequence of size 2 ; If none of the above check is successful then subsequence must be of size 3 ; Never executed ; Driver Code
import java . io . * ; class GFG { public static void findMinimumSubsequence ( String S ) { boolean flag = false ; StringBuilder dummy = new StringBuilder ( ) ; for ( int j = 0 ; j < S . length ( ) ; j ++ ) { if ( S . charAt ( j ) != '2' && S . charAt ( j ) != '3' && S . charAt ( j ) != '5' && S . charAt ( j ) != '7' ) { System . out . println ( 1 ) ; flag = true ; break ; } } if ( ! flag ) { loop : for ( int j = 0 ; j < S . length ( ) - 1 ; j ++ ) { for ( int j1 = j + 1 ; j1 < S . length ( ) ; j1 ++ ) { dummy = new StringBuilder ( Character . toString ( S . charAt ( j ) ) ) ; dummy . append ( S . charAt ( j1 ) ) ; if ( ! dummy . toString ( ) . equals ( "23" ) && ! dummy . toString ( ) . equals ( "37" ) && ! dummy . toString ( ) . equals ( "53" ) && ! dummy . toString ( ) . equals ( "73" ) ) { System . out . println ( 2 ) ; flag = true ; break loop ; } } } } if ( ! flag ) { if ( S . length ( ) >= 3 ) { System . out . println ( 3 ) ; } else { System . out . println ( - 1 ) ; } } } public static void main ( String [ ] args ) { String S = "237" ; findMinimumSubsequence ( S ) ; } }
Count of ways to empty given String by recursively removing all adjacent duplicates | Java program for the above approach ; Define the dp table globally ; Recursive function to calculate the dp values for range [ L , R ] ; The range is odd length ; The state is already calculated ; If the length is 2 ; Total answer for this state ; Variable to store the current answer . ; Remove characters s [ l ] and s [ i ] . ; Initialize all the states of dp to - 1 Initialize all the states of dp to - 1 ; Calculate all Combinations ; Driver Code
import java . io . * ; class GFG { static int [ ] [ ] dp = new int [ 505 ] [ 505 ] ; static int [ ] [ ] choose = new int [ 502 ] [ 502 ] ; static int calc ( int l , int r , String s ) { if ( Math . abs ( r - l ) % 2 == 0 ) { return 0 ; } if ( l > r ) { return dp [ l ] [ r ] = 1 ; } if ( dp [ l ] [ r ] != - 1 ) { return dp [ l ] [ r ] ; } if ( ( r - l ) == 1 ) { if ( s . charAt ( l ) == s . charAt ( r ) ) { dp [ l ] [ r ] = 1 ; } else { dp [ l ] [ r ] = 0 ; } return dp [ l ] [ r ] ; } int ans = 0 ; for ( int k = l + 1 ; k <= r ; k += 2 ) { int temp = 1 ; if ( s . charAt ( l ) == s . charAt ( k ) ) { temp = calc ( l + 1 , k - 1 , s ) * calc ( k + 1 , r , s ) * choose [ ( ( r - l + 1 ) / 2 ) ] [ ( ( r - k ) / 2 ) ] ; ans += temp ; } } return dp [ l ] [ r ] = ans ; } static int waysToClearString ( String S ) { for ( int i = 0 ; i < 505 ; i ++ ) { for ( int j = 0 ; j < 505 ; j ++ ) dp [ i ] [ j ] = - 1 ; } int n = S . length ( ) ; choose [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= ( n / 2 ) ; ++ i ) { choose [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j <= i ; ++ j ) { choose [ i ] [ j ] = ( choose [ i - 1 ] [ j ] + choose [ i - 1 ] [ j - 1 ] ) ; } } return calc ( 0 , n - 1 , S ) ; } public static void main ( String [ ] args ) { String S = " aabccb " ; System . out . println ( waysToClearString ( S ) ) ; } }