codeparrot/xlcost-text-to-code huggingface dataset:
Lexicographically largest string formed from the characters in range L and R | Java program to print the lexicographically largest String that can be formed from the characters in range L and R ; Function to return the lexicographically largest String ; hash array ; make 0 - based indexing ; iterate and count frequencies of character ; ans String ; iterate in frequency array ; add til all characters are added ; Driver Code
class GFG { static String printLargestString ( String s , int l , int r ) { int freq [ ] = new int [ 26 ] ; l -- ; r -- ; for ( int i = Math . min ( l , r ) ; i <= Math . max ( l , r ) ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; } String ans = " " ; for ( int i = 25 ; i >= 0 ; i -- ) { while ( freq [ i ] > 0 ) { ans += ( char ) ( ' a ' + i ) ; freq [ i ] -- ; } } return ans ; } public static void main ( String [ ] args ) { String s = " striver " ; int l = 3 , r = 5 ; System . out . println ( printLargestString ( s , l , r ) ) ; } }
Arrange a binary string to get maximum value within a range of indices | Java implementation of the approach ; Storing the count of 1 's in the String ; Query of l and r ; Applying range update technique . ; Taking prefix sum to get the range update values ; Final array which will store the arranged String ; if after maximizing the ranges any 1 is left then we maximize the String lexicographically . ; Driver Code
class GFG { static void arrange ( String s ) { int cc = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) cc ++ ; } int [ ] a = new int [ s . length ( ) + 1 ] ; int qq [ ] [ ] = { { 2 , 3 } , { 5 , 5 } } ; int n = qq . length ; for ( int i = 0 ; i < n ; i ++ ) { int l = qq [ i ] [ 0 ] , r = qq [ i ] [ 1 ] ; l -- ; r -- ; a [ l ] ++ ; a [ r + 1 ] -- ; } int len_a = a . length ; for ( int i = 1 ; i < len_a ; i ++ ) { a [ i ] += a [ i - 1 ] ; } int [ ] zz = new int [ s . length ( ) ] ; for ( int i = 0 ; i < len_a - 1 ; i ++ ) { if ( a [ i ] > 0 ) { if ( cc > 0 ) { zz [ i ] = 1 ; cc -- ; } else break ; } if ( cc == 0 ) break ; } if ( cc > 0 ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( zz [ i ] == 0 ) { zz [ i ] = 1 ; cc -- ; } if ( cc == 0 ) break ; } } for ( int i = 0 ; i < s . length ( ) ; i ++ ) System . out . print ( zz [ i ] ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { String str = "11100" ; arrange ( str ) ; } }
Check whether the vowels in a string are in alphabetical order or not | Java implementation of above approach ; Function that checks whether the vowel characters in a string are in alphabetical order or not ; ASCII Value 64 is less than all the alphabets so using it as a default value ; check if the vowels in the string are sorted or not ; if the vowel is smaller than the previous vowel ; store the vowel ; Driver code ; check whether the vowel characters in a string are in alphabetical order or not
import java . io . * ; class GFG { static boolean areVowelsInOrder ( String s ) { int n = s . length ( ) ; char c = ( char ) 64 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' a ' || s . charAt ( i ) == ' e ' || s . charAt ( i ) == ' i ' || s . charAt ( i ) == ' o ' || s . charAt ( i ) == ' u ' ) { if ( s . charAt ( i ) < c ) return false ; else { c = s . charAt ( i ) ; } } } return true ; } public static void main ( String [ ] args ) { String s = " aabbbddeecc " ; if ( areVowelsInOrder ( s ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Program to replace every space in a string with hyphen | Java implementation of the above approach ; Split by space and converting String to list and ; returning the string ; Driver code
import java . util . * ; class GFG { static String printHyphen ( String string ) { string = string . replace ( ' ▁ ' , ' - ' ) ; return string ; } public static void main ( String [ ] args ) { String string = " Text ▁ contains ▁ malayalam ▁ and ▁ level ▁ words " ; System . out . print ( printHyphen ( string ) ) ; } }
Rearrange the string to maximize the number of palindromic substrings | Java program to rearrange the string such to maximize the number of palindromic substrings ; Function to return the newString ; length of string ; hashing array ; iterate and count ; resulting string ; form the resulting string ; number of times character appears ; append to resulting string ; Driver code
public class GFG { static String newString ( String s ) { int l = s . length ( ) ; int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] += 1 ; } String ans = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { ans += ( char ) ( 97 + i ) ; } } return ans ; } public static void main ( String args [ ] ) { String s = " aabab " ; System . out . println ( newString ( s ) ) ; } }
Program to find remainder when large number is divided by r | Java implementation to find Remainder when a large Number is divided by R ; Function to Return Remainder ; len is variable to store the length of Number string . ; loop that find Remainder ; Return the remainder ; Driver code ; Get the large number as string ; Get the divisor R ; Find and print the remainder
class GFG { static int Remainder ( String str , int R ) { int len = str . length ( ) ; int Num , Rem = 0 ; for ( int i = 0 ; i < len ; i ++ ) { Num = Rem * 10 + ( str . charAt ( i ) - '0' ) ; Rem = Num % R ; } return Rem ; } public static void main ( String [ ] args ) { String str = "13589234356546756" ; int R = 13 ; System . out . println ( Remainder ( str , R ) ) ; } }
Number of balanced bracket subsequence of length 2 and 4 | Java implementation of above approach ; Taking the frequency suffix sum of the number of 2 's present after every index ; Storing the count of subsequence ; Subsequence of length 2 ; Subsequence of length 4 of type 1 1 2 2 ; Subsequence of length 4 of type 1 2 1 2 ; Driver Code
class GFG { public static void countWays ( int a [ ] , int n ) { int i , j ; long suff [ ] = new long [ n ] ; if ( a [ n - 1 ] == 2 ) suff [ n - 1 ] = 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] == 2 ) suff [ i ] = suff [ i + 1 ] + 1 ; else suff [ i ] = suff [ i + 1 ] ; } long ss = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) ss += suff [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && suff [ j ] >= 2 ) { ss += ( suff [ j ] ) * ( suff [ j ] - 1 ) / 2 ; } } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && ( suff [ i ] - suff [ j ] ) >= 1 && suff [ j ] >= 1 ) { ss += ( suff [ i ] - suff [ j ] ) * suff [ j ] ; } } } System . out . println ( ss ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 1 , 2 , 2 } ; int n = 6 ; countWays ( a , n ) ; } }
Count the number of carry operations required to add two numbers | Java implementation of above approach ; Function to count the number of carry operations ; Initialize the value of carry to 0 ; Counts the number of carry operations ; Initialize len_a and len_b with the sizes of strings ; Assigning the ascii value of the character ; Add both numbers / digits ; If sum > 0 , increment count and set carry to 1 ; Else , set carry to 0 ; Driver code
import java . io . * ; class GFG { static int count_carry ( String a , String b ) { int carry = 0 ; int count = 0 ; int len_a = a . length ( ) , len_b = b . length ( ) ; while ( len_a != 0 len_b != 0 ) { int x = 0 , y = 0 ; if ( len_a > 0 ) { x = a . charAt ( len_a - 1 ) - '0' ; len_a -- ; } if ( len_b > 0 ) { y = b . charAt ( len_b - 1 ) - '0' ; len_b -- ; } int sum = x + y + carry ; if ( sum >= 10 ) { carry = 1 ; count ++ ; } else carry = 0 ; } return count ; } public static void main ( String [ ] args ) { String a = "9555" , b = "555" ; int count = count_carry ( a , b ) ; if ( count == 0 ) System . out . println ( "0NEW_LINE"); else if ( count == 1 ) System . out . println ( "1NEW_LINE"); else System . out . println ( count ) ; } }
Check if a number is in given base or not | Java program to check if given number is in given base or not . ; Allowed bases are till 16 ( Hexadecimal ) ; If base is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver Class
class Geeks { static boolean isInGivenBase ( String str , int base ) { if ( base > 16 ) return false ; else if ( base <= 10 ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( ! ( str . charAt ( i ) >= '0' && str . charAt ( i ) < ( '0' + base ) ) ) return false ; } else { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( ! ( ( str . charAt ( i ) >= '0' && str . charAt ( i ) < ( '0' + base ) ) || ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) < ( ' A ' + base - 10 ) ) ) ) return false ; } return true ; } public static void main ( String args [ ] ) { String str = " AF87" ; if ( isInGivenBase ( str , 16 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find indices of all occurrence of one string in other | Java program to find indices of all occurrences of one String in other . ; Driver code
class GFG { static void printIndex ( String str , String s ) { boolean flag = false ; for ( int i = 0 ; i < str . length ( ) - s . length ( ) + 1 ; i ++ ) { if ( str . substring ( i , i + s . length ( ) ) . equals ( s ) ) { System . out . print ( i + " ▁ " ) ; flag = true ; } } if ( flag == false ) { System . out . println ( " NONE " ) ; } } public static void main ( String [ ] args ) { String str1 = " GeeksforGeeks " ; String str2 = " Geeks " ; printIndex ( str1 , str2 ) ; } }
Alternatively Merge two Strings in Java | Java code to alternatively merge two strings ; Function for alternatively merging two strings ; To store the final string ; For every index in the strings ; First choose the ith character of the first string if it exists ; Then choose the ith character of the second string if it exists ; Driver code
public class mergeString { public static String merge ( String s1 , String s2 ) { StringBuilder result = new StringBuilder ( ) ; for ( int i = 0 ; i < s1 . length ( ) || i < s2 . length ( ) ; i ++ ) { if ( i < s1 . length ( ) ) result . append ( s1 . charAt ( i ) ) ; if ( i < s2 . length ( ) ) result . append ( s2 . charAt ( i ) ) ; } return result . toString ( ) ; } public static void main ( String [ ] args ) { String s1 = " geeks " ; String s2 = " forgeeks " ; System . out . println ( merge ( s1 , s2 ) ) ; } }
Maximum occurring character in an input string | Set | Java implementation to find the maximum occurring character in an input string which is lexicographically first ; function to find the maximum occurring character in an input string which is lexicographically first ; freq [ ] used as hash table ; to store maximum frequency ; to store the maximum occurring character ; length of ' str ' ; get frequency of each character of ' str ' ; for each character , where character is obtained by ( i + ' a ' ) check whether it is the maximum character so far and accodingly update ' result ' ; maximum occurring character ; Driver Code
class GFG { static char getMaxOccurringChar ( char str [ ] ) { int freq [ ] = new int [ 26 ] ; int max = - 1 ; char result = 0 ; int len = str . length ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] != ' ▁ ' ) { freq [ str [ i ] - ' a ' ] ++ ; } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( max < freq [ i ] ) { max = freq [ i ] ; result = ( char ) ( i + ' a ' ) ; } } return result ; } public static void main ( String [ ] args ) { char str [ ] = " sample ▁ program " . toCharArray ( ) ; System . out . println ( " Maximum ▁ occurring ▁ character ▁ = ▁ " + getMaxOccurringChar ( str ) ) ; } }
Given two numbers as strings , find if one is a power of other | Java program to check if one number is power of other ; Multiply the numbers . It multiplies each digit of second string to each digit of first and stores the result . ; If the digit exceeds 9 , add the cumulative carry to previous digit . ; if all zeroes , return "0" . ; Remove starting zeroes . ; Removes Extra zeroes from front of a string . ; Make sure there are no leading zeroes in the string . ; Making sure that s1 is smaller . If it is greater , we recur we reversed parameters . ; Driver Code
import java . util . * ; class new_file { static boolean isGreaterThanEqual ( String s1 , String s2 ) { if ( s1 . length ( ) > s2 . length ( ) ) return true ; if ( s1 . compareTo ( s2 ) == 0 ) return true ; return false ; } static String multiply ( String s1 , String s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; int [ ] result = new int [ n + m ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) for ( int j = m - 1 ; j >= 0 ; j -- ) result [ i + j + 1 ] += ( s1 . charAt ( i ) - '0' ) * ( s2 . charAt ( j ) - '0' ) ; int size = result . length ; for ( int i = size - 1 ; i > 0 ; i -- ) { if ( result [ i ] >= 10 ) { result [ i - 1 ] += result [ i ] / 10 ; result [ i ] = result [ i ] % 10 ; } } int i = 0 ; while ( i < size && result [ i ] == 0 ) i ++ ; if ( i == size ) return "0" ; String temp = " " ; while ( i < size ) { temp += Integer . toString ( result [ i ] ) ; i ++ ; } return temp ; } static String removeLeadingZeores ( String s ) { int n = s . length ( ) ; int i = 0 ; while ( i < n && s . charAt ( i ) == '0' ) i ++ ; if ( i == n ) return "0" ; String temp = " " ; while ( i < n ) { temp += s . charAt ( i ) ; i ++ ; } return temp ; } static boolean isPower ( String s1 , String s2 ) { s1 = removeLeadingZeores ( s1 ) ; s2 = removeLeadingZeores ( s2 ) ; if ( s1 == "0" s2 == "0" ) return false ; if ( s1 == "1" && s2 == "1" ) return true ; if ( s1 == "1" s2 == "1" ) return true ; if ( s1 . length ( ) > s2 . length ( ) ) return isPower ( s2 , s1 ) ; String temp = new String ( s1 ) ; while ( ! isGreaterThanEqual ( s1 , s2 ) ) s1 = multiply ( s1 , temp ) ; if ( s1 . compareTo ( s2 ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String s1 = "374747" , s2 = "52627712618930723" ; if ( isPower ( s1 , s2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; s1 = "4099" ; s2 = "2" ; if ( isPower ( s1 , s2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check for balanced parentheses in an expression | O ( 1 ) space | Java code to check balanced parentheses with O ( 1 ) space . ; Function1 to match closing bracket ; Function1 to match opening bracket ; Function to check balanced parentheses ; helper variables ; Handling case of opening parentheses ; } Handling case of closing parentheses ; If corresponding matching opening parentheses doesn 't  lie in given interval return 0 ; else continue ; If corresponding closing parentheses doesn 't lie in given interval  return 0 ; if found , now check for each opening and closing parentheses in this interval ; Driver Code
class GFG { static int matchClosing ( char X [ ] , int start , int end , char open , char close ) { int c = 1 ; int i = start + 1 ; while ( i <= end ) { if ( X [ i ] == open ) { c ++ ; } else if ( X [ i ] == close ) { c -- ; } if ( c == 0 ) { return i ; } i ++ ; } return i ; } static int matchingOpening ( char X [ ] , int start , int end , char open , char close ) { int c = - 1 ; int i = end - 1 ; while ( i >= start ) { if ( X [ i ] == open ) { c ++ ; } else if ( X [ i ] == close ) { c -- ; } if ( c == 0 ) { return i ; } i -- ; } return - 1 ; } static boolean isBalanced ( char X [ ] , int n ) { int i , j = 0 , k , x , start , end ; for ( i = 0 ; i < n ; i ++ ) { if ( X [ i ] == ' ( ' ) { j = matchClosing ( X , i , n - 1 , ' ( ' , ' ) ' ) ; } else if ( X [ i ] == ' { ' ) { j = matchClosing ( X , i , n - 1 , ' { ' , ' } ' ) ; } else if ( X [ i ] == ' [ ' ) { j = matchClosing ( X , i , n - 1 , ' [ ' , ' ] ' ) ; } else { if ( X [ i ] == ' ) ' ) { j = matchingOpening ( X , 0 , i , ' ( ' , ' ) ' ) ; } else if ( X [ i ] == ' } ' ) { j = matchingOpening ( X , 0 , i , ' { ' , ' } ' ) ; } else if ( X [ i ] == ' ] ' ) { j = matchingOpening ( X , 0 , i , ' [ ' , ' ] ' ) ; } if ( j < 0 j >= i ) { return false ; } continue ; } if ( j >= n j < 0 ) { return false ; } start = i ; end = j ; for ( k = start + 1 ; k < end ; k ++ ) { if ( X [ k ] == ' ( ' ) { x = matchClosing ( X , k , end , ' ( ' , ' ) ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ) ' ) { x = matchingOpening ( X , start , k , ' ( ' , ' ) ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' { ' ) { x = matchClosing ( X , k , end , ' { ' , ' } ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' } ' ) { x = matchingOpening ( X , start , k , ' { ' , ' } ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' [ ' ) { x = matchClosing ( X , k , end , ' [ ' , ' ] ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ] ' ) { x = matchingOpening ( X , start , k , ' [ ' , ' ] ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } } } return true ; } public static void main ( String [ ] args ) { char X [ ] = " [ ( ) ] ( ) " . toCharArray ( ) ; int n = 6 ; if ( isBalanced ( X , n ) ) System . out . printf ( "YesNEW_LINE"); else System . out . printf ( "NoNEW_LINE"); char Y [ ] = " [ [ ( ) ] ] ) " . toCharArray ( ) ; n = 7 ; if ( isBalanced ( Y , n ) ) System . out . printf ( "YesNEW_LINE"); else System . out . printf ( "NoNEW_LINE"); } }
Sorting array with conditional swapping | Java program to Check if it is possible to sort the array in ascending order . ; Function to check if it is possible to sort the array ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driven Program
import java . util . * ; import java . lang . * ; class GFG { public static String possibleToSort ( int arr [ ] , int n , String str ) { int max_element = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { max_element = Math . max ( max_element , arr [ i ] ) ; if ( str . charAt ( i ) == '0' ) { if ( max_element > i + 1 ) return " No " ; } } return " Yes " ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 5 , 3 , 4 , 6 } ; int n = arr . length ; String str = "01110" ; System . out . println ( possibleToSort ( arr , n , str ) ) ; } }
Find sub | Java program to find substring with given power ; Function to print indexes of substring with power as given power . ; Create an empty map ; Maintains sum of powers of characters so far . ; Add current character power to curr_power . ; If curr_power is equal to target power we found a substring starting from index 0 and ending at index i . ; If curr_power - power already exists in map then we have found a subarray with target power . ; If we reach here , then no substring exists . ; Driver Code
import java . util . * ; class GFG { static void findSubstring ( String str , int power ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; int curr_power = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { curr_power = curr_power + ( str . charAt ( i ) - ' a ' + 1 ) ; if ( curr_power == power ) { System . out . println ( " Substring ▁ from ▁ index ▁ 0" + " ▁ to ▁ " + i + " ▁ has ▁ power ▁ " + power ) ; return ; } if ( map . containsKey ( curr_power - power ) ) { System . out . println ( " Substring ▁ from ▁ index ▁ " + ( map . get ( curr_power - power ) + 1 ) + " ▁ to ▁ " + i + " ▁ has ▁ power ▁ " + power ) ; return ; } map . put ( curr_power , i ) ; } System . out . println ( " No ▁ substring ▁ with ▁ given ▁ power ▁ exists . " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int power = 36 ; findSubstring ( str , power ) ; } }
Prime String | Java program to find if string is a Prime or not . ; Function that checks if sum is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver code
import java . io . * ; class GFG { static boolean isPrimeString ( String str ) { int len = str . length ( ) , n = 0 ; for ( int i = 0 ; i < len ; i ++ ) n += ( int ) str . charAt ( i ) ; if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " geekRam " ; if ( isPrimeString ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Ways to split string such that each partition starts with distinct character | Java Program to find number of way to split string such that each partition starts with distinct character with maximum number of partitions . ; Returns the number of we can split the string ; Finding the frequency of each character . ; making frequency of first character of string equal to 1. ; Finding the product of frequency of occurrence of each character . ; Driver Code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int countWays ( String s ) { int count [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) count [ s . charAt ( i ) - ' a ' ] ++ ; count [ s . charAt ( 0 ) - ' a ' ] = 1 ; int ans = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) if ( count [ i ] != 0 ) ans *= count [ i ] ; return ans ; } public static void main ( String ags [ ] ) { String s = " acbbcc " ; System . out . println ( countWays ( s ) ) ; } }
Lexicographically next greater string using same character set | Java implementation of above algorithm . ; function to print output ; to store unique characters of the string ; to check uniqueness ; if mp [ s [ i ] ] = 0 then it is first time ; sort the unique characters ; simply add n - k smallest characters ; return ; end the program ; searching the first character left of index k and not equal to greatest character of the string ; finding the just next greater character than s [ i ] ; suffix with smallest character ; if we reach here then all indices to the left of k had the greatest character ; Driver code ; Function call
import java . util . * ; class GFG { static void lexoString ( char [ ] s , int k ) { int n = s . length ; Vector < Character > v = new Vector < > ( ) ; Map < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( ! mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , 1 ) ; v . add ( s [ i ] ) ; } } Collections . sort ( v ) ; if ( k > n ) { System . out . print ( String . valueOf ( s ) ) ; for ( int i = n ; i < k ; i ++ ) { System . out . print ( v . get ( 0 ) ) ; } } for ( int i = k - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != v . get ( v . size ( ) - 1 ) ) { for ( int j = 0 ; j < i ; j ++ ) { System . out . print ( s [ j ] ) ; } for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( v . get ( j ) > s [ i ] ) { System . out . print ( v . get ( j ) ) ; break ; } } for ( int j = i + 1 ; j < k ; j ++ ) { System . out . print ( v . get ( 0 ) ) ; } return ; } } System . out . print ( " No ▁ lexicographically ▁ greater ▁ string ▁ of ▁ length ▁ " + k + " ▁ possible ▁ here . " ) ; } public static void main ( String arr [ ] ) { String s = " gi " ; int k = 3 ; lexoString ( s . toCharArray ( ) , k ) ; } }