codeparrot/xlcost-text-to-code huggingface dataset:
Find the count of sub | Java implementation of the approach ; Function to return the count of required occurrence ; To store the count of occurrences ; Check first four characters from ith position ; Variables for counting the required characters ; Check the four contiguous characters which can be reordered to form ' clap ' ; If all four contiguous characters are present then increment cnt variable ; Driver code
class GFG { static int countOcc ( String s ) { int cnt = 0 ; for ( int i = 0 ; i < s . length ( ) - 3 ; i ++ ) { int c = 0 , l = 0 , a = 0 , p = 0 ; for ( int j = i ; j < i + 4 ; j ++ ) { switch ( s . charAt ( j ) ) { case ' c ' : c ++ ; break ; case ' l ' : l ++ ; break ; case ' a ' : a ++ ; break ; case ' p ' : p ++ ; break ; } } if ( c == 1 && l == 1 && a == 1 && p == 1 ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String s = " clapc " ; System . out . print ( countOcc ( s . toLowerCase ( ) ) ) ; } }
Number of ways in which the substring in range [ L , R ] can be formed using characters out of the range | Java implementation of the approach ; Function to return the number of ways to form the sub - string ; Initialize a hash - table with 0 ; Iterate in the string and count the frequency of characters that do not lie in the range L and R ; Out of range characters ; Stores the final number of ways ; Iterate for the sub - string in the range L and R ; If exists then multiply the number of ways and decrement the frequency ; If does not exist the sub - string cannot be formed ; Return the answer ; Driver code
class GfG { static int calculateWays ( String s , int n , int l , int r ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < l i > r ) freq [ s . charAt ( i ) - ' a ' ] ++ ; } int ways = 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( freq [ s . charAt ( i ) - ' a ' ] != 0 ) { ways = ways * freq [ s . charAt ( i ) - ' a ' ] ; freq [ s . charAt ( i ) - ' a ' ] -- ; } else { ways = 0 ; break ; } } return ways ; } public static void main ( String [ ] args ) { String s = " cabcaab " ; int n = s . length ( ) ; int l = 1 , r = 3 ; System . out . println ( calculateWays ( s , n , l , r ) ) ; } }
Program to find the kth character after decrypting a string | Java implementation of the approach ; Function to print kth character of String s after decrypting it ; Get the length of string ; Initialise pointer to character of input string to zero ; Total length of resultant string ; Traverse the string from starting and check if each character is alphabet then increment total_len ; If total_leg equal to k then return string else increment i ; Parse the number ; Update next_total_len ; Get the position of kth character ; Position not found then update position with total_len ; Recursively find the kth position ; Else update total_len by next_total_len ; Return - 1 if character not found ; Driver code
import java . util . * ; class GfG { static Character findKthChar ( String s , int k ) { int len = s . length ( ) ; int i = 0 ; int total_len = 0 ; while ( i < len ) { if ( Character . isLetter ( s . charAt ( i ) ) ) { total_len ++ ; if ( total_len == k ) return s . charAt ( i ) ; i ++ ; } else { int n = 0 ; while ( i < len && ! Character . isLetter ( s . charAt ( i ) ) ) { n = n * 10 + ( s . charAt ( i ) - '0' ) ; i ++ ; } int next_total_len = total_len * n ; if ( k <= next_total_len ) { int pos = k % total_len ; if ( pos == 0 ) { pos = total_len ; } return findKthChar ( s , pos ) ; } else { total_len = next_total_len ; } } } return ' ▁ ' ; } public static void main ( String [ ] args ) { String s = " ab2c3" ; int k = 5 ; System . out . println ( findKthChar ( s , k ) ) ; } }
Check whether the Average Character of the String is present or not | Java program to check if the average character is present in the string or not ; Checks if the character is present ; Get the length of string ; Iterate from i = 0 to the length of the string to check if the character is present in the string ; Finds the average character of the string ; Calculate the sum of ASCII values of each character ; Calculate the average of ASCII values ; Convert the ASCII value to character and return it ; Driver code ; Get the average character ; Check if the average character is present in string or not
import java . math . * ; class GFG { static boolean check_char ( String st , char ch ) { int l = st . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( st . charAt ( i ) == ch ) return true ; } return false ; } static char find_avg ( String st ) { int i , sm = 0 ; int l = st . length ( ) ; char ch ; for ( i = 0 ; i < l ; i ++ ) { ch = st . charAt ( i ) ; sm = sm + ( int ) ( ch ) ; } int avg = ( int ) ( Math . floor ( sm / l ) ) ; return ( ( char ) ( avg ) ) ; } public static void main ( String [ ] args ) { String st = " ag23sdfa " ; char ch = find_avg ( st ) ; System . out . println ( ch ) ; if ( check_char ( st , ch ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Convert the ASCII value sentence to its equivalent string | Java implementation of the approach ; Function to print the character sequence for the given ASCII sentence ; Append the current digit ; If num is within the required range ; Convert num to char ; Reset num to 0 ; Driver code
class GFG { static void asciiToSentence ( String str , int len ) { int num = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = num * 10 + ( str . charAt ( i ) - '0' ) ; if ( num >= 32 && num <= 122 ) { char ch = ( char ) num ; System . out . print ( ch ) ; num = 0 ; } } } public static void main ( String args [ ] ) { String str = "7110110110711510211111471101101107115" ; int len = str . length ( ) ; asciiToSentence ( str , len ) ; } }
Distinct state codes that appear in a string as contiguous sub | Java implementation of the above approach . ; Function to return the count of distinct state codes ; Insert every sub - String of length 2 in the set ; Return the size of the set ; Driver code
import java . util . * ; class GFG { static int countDistinctCode ( String str ) { Set < String > codes = new HashSet < > ( ) ; for ( int i = 0 ; i < str . length ( ) - 1 ; i ++ ) codes . add ( str . substring ( i , i + 2 ) ) ; return codes . size ( ) ; } public static void main ( String [ ] args ) { String str = " UPUP " ; System . out . println ( countDistinctCode ( str ) ) ; } }
Count of buttons pressed in a keypad mobile | Java implementation of the approach ; Array to store how many times a button has to be pressed for typing a particular character ; Function to return the count of buttons pressed to type the given string ; Count the key presses ; Return the required count ; Driver code
class GFG { static final int arr [ ] = { 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 1 , 2 , 3 , 4 } ; public static int countKeyPressed ( String str , int len ) { int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) count = count + arr [ str . charAt ( i ) - ' a ' ] ; return count ; } public static void main ( String [ ] args ) { String str = " abcdef " ; int len = str . length ( ) ; System . out . print ( countKeyPressed ( str , len ) ) ; } }
First string from the given array whose reverse is also present in the same array | Java implementation of the approach ; Function that returns true if s1 is equal to reverse of s2 ; If both the strings differ in length ; In case of any character mismatch ; Function to return the first word whose reverse is also present in the array ; Check every string ; Pair with every other string appearing after the current string ; If first string is equal to the reverse of the second string ; No such string exists ; Driver code
class GFG { static boolean isReverseEqual ( String s1 , String s2 ) { if ( s1 . length ( ) != s2 . length ( ) ) return false ; int len = s1 . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( s1 . charAt ( i ) != s2 . charAt ( len - i - 1 ) ) return false ; return true ; } static String getWord ( String str [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( isReverseEqual ( str [ i ] , str [ j ] ) ) return str [ i ] ; return " - 1" ; } public static void main ( String [ ] args ) { String str [ ] = { " geeks " , " for " , " skeeg " } ; int n = str . length ; System . out . print ( getWord ( str , n ) ) ; } }
Check if the given string is K | Java implementation of the approach ; Function that return true if sub - string of length k starting at index i is also a prefix of the string ; k length sub - string cannot start at index i ; Character mismatch between the prefix and the sub - string starting at index i ; Function that returns true if str is K - periodic ; Check whether all the sub - strings str [ 0 , k - 1 ] , str [ k , 2 k - 1 ] ... are equal to the k length prefix of the string ; Driver code
class GFG { static boolean isPrefix ( String str , int len , int i , int k ) { if ( i + k > len ) return false ; for ( int j = 0 ; j < k ; j ++ ) { if ( str . charAt ( i ) != str . charAt ( j ) ) return false ; i ++ ; } return true ; } static boolean isKPeriodic ( String str , int len , int k ) { for ( int i = k ; i < len ; i += k ) if ( ! isPrefix ( str , len , i , k ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " geeksgeeks " ; int len = str . length ( ) ; int k = 5 ; if ( isKPeriodic ( str , len , k ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Minimum number of letters needed to make a total of n | Java implementation of the approach ; Function to return the minimum letters required to make a total of n ; Driver code
class GFG { static int minLettersNeeded ( int n ) { if ( n % 26 == 0 ) return ( n / 26 ) ; else return ( ( n / 26 ) + 1 ) ; } public static void main ( String args [ ] ) { int n = 52 ; System . out . print ( minLettersNeeded ( n ) ) ; } }
Find the first maximum length even word from a string | Java program to find maximum length even word ; Function to find maximum length even word ; To store length of current word . ; To store length of maximum length word . ; To store starting index of maximum length word . ; If current character is space then word has ended . Check if it is even length word or not . If yes then compare length with maximum length found so far . ; Set currlen to zero for next word . ; Update length of current word . ; Check length of last word . ; If no even length word is present then return - 1. ; Driver code
class GFG { static String findMaxLenEven ( String str ) { int n = str . length ( ) ; int i = 0 ; int currlen = 0 ; int maxlen = 0 ; int st = - 1 ; while ( i < n ) { if ( str . charAt ( i ) == ' ▁ ' ) { if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } currlen = 0 ; } else { currlen ++ ; } i ++ ; } if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } if ( st == - 1 ) return " - 1" ; return str . substring ( st , st + maxlen ) ; } public static void main ( String args [ ] ) { String str = " this ▁ is ▁ a ▁ test ▁ string " ; System . out . println ( findMaxLenEven ( str ) ) ; } }
Minimum length substring with exactly K distinct characters | Java program to find minimum length subString having exactly k distinct character . ; Function to find minimum length subString having exactly k distinct character . ; Starting index of sliding window . ; Ending index of sliding window . ; To store count of character . ; To store count of distinct character in current sliding window . ; To store length of current sliding window . ; To store minimum length . ; To store starting index of minimum length subString . ; Increment count of current character If this count is one then a new distinct character is found in sliding window . ; If number of distinct characters is is greater than k , then move starting point of sliding window forward , until count is k . ; Remove characters from the beginning of sliding window having count more than 1 to minimize length . ; Compare length with minimum length and update if required . ; Return minimum length subString . ; Driver code
class GFG { static String findMinLenStr ( String str , int k ) { int n = str . length ( ) ; int st = 0 ; int end = 0 ; int cnt [ ] = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) cnt [ i ] = 0 ; int distEle = 0 ; int currlen ; int minlen = n ; int startInd = - 1 ; while ( end < n ) { cnt [ str . charAt ( end ) - ' a ' ] ++ ; if ( cnt [ str . charAt ( end ) - ' a ' ] == 1 ) distEle ++ ; if ( distEle > k ) { while ( st < end && distEle > k ) { if ( cnt [ str . charAt ( st ) - ' a ' ] == 1 ) distEle -- ; cnt [ str . charAt ( st ) - ' a ' ] -- ; st ++ ; } } if ( distEle == k ) { while ( st < end && cnt [ str . charAt ( st ) - ' a ' ] > 1 ) { cnt [ str . charAt ( st ) - ' a ' ] -- ; st ++ ; } currlen = end - st + 1 ; if ( currlen < minlen ) { minlen = currlen ; startInd = st ; } } end ++ ; } return str . substring ( startInd , startInd + minlen ) ; } public static void main ( String args [ ] ) { String str = " efecfefd " ; int k = 4 ; System . out . println ( findMinLenStr ( str , k ) ) ; } }
Minimum number of replacements to make the binary string alternating | Set 2 | Java implementation of the approach ; Function to return the minimum number of characters of the given binary string to be replaced to make the string alternating ; If there is 1 at even index positions ; If there is 0 at odd index positions ; Driver code
class GFG { static int minReplacement ( String s , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) == '1' ) ans ++ ; if ( i % 2 == 1 && s . charAt ( i ) == '0' ) ans ++ ; } return Math . min ( ans , len - ans ) ; } public static void main ( String args [ ] ) { String s = "1100" ; int len = s . length ( ) ; System . out . print ( minReplacement ( s , len ) ) ; } }
Deletions of "01" or "10" in binary string to make it free from "01" or "10" | Java implementation of the approach ; Function to return the count of deletions of sub - strings "01" or "10" ; To store the count of 0 s and 1 s ; Driver code
class GFG { static int substrDeletion ( String str , int len ) { int count0 = 0 , count1 = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) count0 ++ ; else count1 ++ ; } return Math . min ( count0 , count1 ) ; } public static void main ( String [ ] args ) { String str = "010" ; int len = str . length ( ) ; System . out . println ( substrDeletion ( str , len ) ) ; } }
Group consecutive characters of same type in a string | Java implementation of the approach ; Function to return the modified String ; Store original String ; Remove all white spaces ; To store the resultant String ; Traverse the String ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant String ; Driver code
class GFG { static String groupCharacters ( char [ ] s , int len ) { String temp = " " ; for ( int i = 0 ; i < len ; i ++ ) if ( s [ i ] != ' ▁ ' ) temp = temp + s [ i ] ; len = temp . length ( ) ; String ans = " " ; int i = 0 ; while ( i < len ) { if ( temp . charAt ( i ) >= ( ' A ' ) && temp . charAt ( i ) <= ( ' Z ' ) ) { while ( i < len && temp . charAt ( i ) >= ( ' A ' ) && temp . charAt ( i ) <= ( ' Z ' ) ) { ans = ans + temp . charAt ( i ) ; i ++ ; } ans = ans + " ▁ " ; } else if ( temp . charAt ( i ) >= ( '0' ) && temp . charAt ( i ) <= ( '9' ) ) { while ( i < len && temp . charAt ( i ) >= ( '0' ) && temp . charAt ( i ) <= ( '9' ) ) { ans = ans + temp . charAt ( i ) ; i ++ ; } ans = ans + " ▁ " ; } else { while ( i < len && temp . charAt ( i ) >= ( ' * ' ) && temp . charAt ( i ) <= ( ' / ' ) ) { ans = ans + temp . charAt ( i ) ; i ++ ; } ans = ans + " ▁ " ; } } return ans ; } public static void main ( String [ ] args ) { String s = "34FTG234 + ▁ + - ▁ * " ; int len = s . length ( ) ; System . out . print ( groupCharacters ( s . toCharArray ( ) , len ) ) ; } }
Find the minimum number of preprocess moves required to make two strings equal | Java implementation of the approach ; Function to return the minimum number of pre - processing moves required on string A ; Length of the given strings ; To store the required answer ; Run a loop upto n / 2 ; To store frequency of 4 characters ; If size is 4 ; If size is 3 ; If size is 2 ; If n is odd ; Driver code
import java . util . * ; class GFG { static int Preprocess ( String A , String B ) { int n = A . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { HashMap < Character , Integer > mp = new HashMap < > ( ) ; if ( mp . containsKey ( A . charAt ( i ) ) ) mp . put ( A . charAt ( i ) , mp . get ( A . charAt ( i ) ) + 1 ) ; else mp . put ( A . charAt ( i ) , 1 ) ; if ( mp . containsKey ( A . charAt ( n - i - 1 ) ) ) mp . put ( A . charAt ( n - i - 1 ) , mp . get ( A . charAt ( n - i - 1 ) ) + 1 ) ; else mp . put ( A . charAt ( n - i - 1 ) , 1 ) ; if ( mp . containsKey ( B . charAt ( i ) ) ) mp . put ( B . charAt ( i ) , mp . get ( B . charAt ( i ) ) + 1 ) ; else mp . put ( B . charAt ( i ) , 1 ) ; if ( mp . containsKey ( B . charAt ( n - i - 1 ) ) ) mp . put ( B . charAt ( n - i - 1 ) , mp . get ( B . charAt ( n - i - 1 ) ) + 1 ) ; else mp . put ( B . charAt ( n - i - 1 ) , 1 ) ; int sz = mp . size ( ) ; if ( sz == 4 ) ans += 2 ; else if ( sz == 3 ) ans += 1 + ( A . charAt ( i ) == A . charAt ( n - i - 1 ) ? 1 : 0 ) ; else if ( sz == 2 ) ans += mp . get ( A . charAt ( i ) ) != 2 ? 1 : 0 ; } if ( n % 2 == 1 && A . charAt ( n / 2 ) != B . charAt ( n / 2 ) ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { String A = " abacaba " , B = " bacabaa " ; System . out . println ( Preprocess ( A , B ) ) ; } }
Check whether two strings are equivalent or not according to given condition | Java Program to find whether two strings are equivalent or not according to given condition ; This function returns the least lexicogr aphical String obtained from its two halves ; Base Case - If String size is 1 ; Divide the String into its two halves ; Form least lexicographical String ; Driver Code
class GfG { static String leastLexiString ( String s ) { if ( s . length ( ) == 1 ) return s ; String x = leastLexiString ( s . substring ( 0 , s . length ( ) / 2 ) ) ; String y = leastLexiString ( s . substring ( s . length ( ) / 2 ) ) ; return String . valueOf ( ( x + y ) . compareTo ( y + x ) ) ; } static boolean areEquivalent ( String a , String b ) { return ! ( leastLexiString ( a ) . equals ( leastLexiString ( b ) ) ) ; } public static void main ( String [ ] args ) { String a = " aaba " ; String b = " abaa " ; if ( areEquivalent ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; a = " aabb " ; b = " abab " ; if ( areEquivalent ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if a string can be converted to another string by replacing vowels and consonants | Java program to check if a string can be converted to other string by replacing vowels and consonants ; Function to check if the character is vowel or not ; Function that checks if a string can be converted to another string ; Find length of string ; If length is not same ; Iterate for every character ; If both vowel ; Both are consonants ; Driver Code
class GfG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static boolean checkPossibility ( String s1 , String s2 ) { int l1 = s1 . length ( ) ; int l2 = s2 . length ( ) ; if ( l1 != l2 ) return false ; for ( int i = 0 ; i < l1 ; i ++ ) { if ( isVowel ( s1 . charAt ( i ) ) && isVowel ( s2 . charAt ( i ) ) ) continue ; else if ( ! ( isVowel ( s1 . charAt ( i ) ) ) && ! ( isVowel ( s2 . charAt ( i ) ) ) ) continue ; else return false ; } return true ; } public static void main ( String [ ] args ) { String S1 = " abcgle " ; String S2 = " ezggli " ; if ( checkPossibility ( S1 , S2 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Generate a string consisting of characters ' a ' and ' b ' that satisfy the given conditions | Java implementation of the approach ; Function to generate and print the required string ; More ' b ' , append " bba " ; More ' a ' , append " aab " ; Equal number of ' a ' and ' b ' append " ab " ; Driver code
class GFG { static void generateString ( int A , int B ) { String rt = " " ; while ( 0 < A 0 < B ) { if ( A < B ) { if ( 0 < B -- ) { rt += ( ' b ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } if ( 0 < A -- ) { rt += ( ' a ' ) ; } } else if ( B < A ) { if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } } else { if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } } } System . out . println ( rt ) ; } public static void main ( String [ ] args ) { int A = 2 , B = 6 ; generateString ( A , B ) ; } }
Number of strings that satisfy the given condition | Java implementation of the approach ; Function to return the count of valid strings ; Set to store indices of valid strings ; Find the maximum digit for current position ; Add indices of all the strings in the set that contain maximal digit ; Return number of strings in the set ; Driver code
import java . util . * ; class GfG { static int countStrings ( int n , int m , String s [ ] ) { HashSet < Integer > ind = new HashSet < Integer > ( ) ; for ( int j = 0 ; j < m ; j ++ ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) mx = Math . max ( mx , ( int ) ( s [ i ] . charAt ( j ) - '0' ) ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] . charAt ( j ) - '0' == mx ) ind . add ( i ) ; } return ind . size ( ) ; } public static void main ( String [ ] args ) { String s [ ] = { "223" , "232" , "112" } ; int m = s [ 0 ] . length ( ) ; int n = s . length ; System . out . println ( countStrings ( n , m , s ) ) ; } }
Lexicographically largest sub | Java implementation of the approach ; Function to return the lexicographically largest sub - sequence of s ; Get the max character from the String ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining String ; Driver code
class GFG { static String getSubSeq ( String s , int n ) { String res = " " ; int cr = 0 ; while ( cr < n ) { char mx = s . charAt ( cr ) ; for ( int i = cr + 1 ; i < n ; i ++ ) { mx = ( char ) Math . max ( mx , s . charAt ( i ) ) ; } int lst = cr ; for ( int i = cr ; i < n ; i ++ ) { if ( s . charAt ( i ) == mx ) { res += s . charAt ( i ) ; lst = i ; } } cr = lst + 1 ; } return res ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . length ( ) ; System . out . println ( getSubSeq ( s , n ) ) ; } }
Minimum replacements such that the difference between the index of the same characters is divisible by 3 | Java program to find minimum replacements such that the difference between the index of the same characters is divisible by 3 ; Function to count the number of minimal replacements ; Generate all permutations ; Count the replacements ; Return the replacements ; Driver Code
class GFG { static int countMinimalReplacements ( String s ) { int n = s . length ( ) ; int mini = Integer . MAX_VALUE ; char [ ] dup = "012" . toCharArray ( ) ; do { int dif = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) != dup [ i % 3 ] ) { dif ++ ; } } mini = Math . min ( mini , dif ) ; } while ( next_permutation ( dup ) ) ; return mini ; } static boolean next_permutation ( char [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) { if ( p [ a ] < p [ a + 1 ] ) { for ( int b = p . length - 1 ; ; -- b ) { if ( p [ b ] > p [ a ] ) { char t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } } } } return false ; } public static void main ( String args [ ] ) { String s = "2101200" ; System . out . println ( countMinimalReplacements ( s ) ) ; } }