codeparrot/xlcost-text-to-code huggingface dataset:
Length of the longest substring with every character appearing even number of times | Java program for the above approach ; Function to find length of the longest subString with each element occurring even number of times ; Initialize unordered_map ; Stores the length of the longest required subString ; Traverse the String ; Stores the value of the digit present at current index ; Bitwise XOR of the mask with 1 left - shifted by val ; Check if the value of mask is already present in ind or not ; Update the final answer ; Otherwise ; Return the answer ; Driver Code ; Given String ; Length of the given String ; Function Call
import java . util . * ; class GFG { static int lenOfLongestReqSubstr ( String s , int N ) { HashMap < Integer , Integer > ind = new HashMap < > ( ) ; int mask = 0 ; ind . put ( 0 , - 1 ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int val = s . charAt ( i ) - '0' ; mask ^= ( 1 << val ) ; if ( ind . containsKey ( mask ) ) { ans = Math . max ( ans , i - ind . get ( mask ) ) ; } else ind . put ( mask , i ) ; } return ans ; } public static void main ( String [ ] args ) { String s = "223015150" ; int N = s . length ( ) ; System . out . print ( lenOfLongestReqSubstr ( s , N ) ) ; } }
Count distinct strings possible by replacing each character by its Morse code | Java program to implement the above approach ; Function to count unique array elements by replacing each character by its Morse code ; Stores Morse code of all lowercase characters ; Stores distinct elements of String by replacing each character by Morse code ; Stores length of arr [ ] array ; Traverse the array ; Stores the Morse code of arr [ i ] ; Stores length of current String ; Update temp ; Insert temp into st ; Return count of elements in the set ; Driver code
import java . util . * ; class GFG { static int uniqueMorseRep ( String [ ] arr ) { String [ ] morseCode = { " . - " , " - . . . " , " - . - . " , " - . . " , " . " , " . . - . " , " - - . " , " . . . . " , " . . " , " . - - - " , " - . - " , " . - . . " , " - - " , " - . " , " - - - " , " . - - . " , " - - . - " , " . - . " , " . . . " , " - " , " . . - " , " . . . - " , " . - - " , " - . . - " , " - . - - " , " - - . . " } ; HashSet < String > st = new HashSet < > ( ) ; int N = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { String temp = " " ; int M = arr [ i ] . length ( ) ; for ( int j = 0 ; j < M ; j ++ ) { temp += morseCode [ arr [ i ] . charAt ( j ) - ' a ' ] ; } st . add ( temp ) ; } return st . size ( ) ; } public static void main ( String [ ] args ) { String [ ] arr = { " gig " , " zeg " , " gin " , " msn " } ; System . out . print ( uniqueMorseRep ( arr ) + "NEW_LINE"); } }
Maximum repeating character for every index in given String | Java program to implement the above approach ; Function to print the maximum repeating character at each index of the string ; Stores frequency of each distinct character ; Stores frequency of maximum repeating character ; Stores the character having maximum frequency ; Traverse the string ; Stores current character ; Update the frequency of str [ i ] ; If frequency of current character exceeds max ; Update max ; Update charMax ; Print the required output ; Driver Code ; Stores length of str
import java . util . * ; public class GFG { public static void findFreq ( String str , int N ) { int [ ] freq = new int [ 256 ] ; int max = 0 ; char charMax = '0' ; for ( int i = 0 ; i < N ; i ++ ) { char ch = str . charAt ( i ) ; freq [ ch ] ++ ; if ( freq [ ch ] >= max ) { max = freq [ ch ] ; charMax = ch ; } System . out . println ( charMax + " ▁ - > ▁ " + max ) ; } } public static void main ( String [ ] args ) { String str = " abbc " ; int N = str . length ( ) ; findFreq ( str , N ) ; } }
Minimize cost to convert given string to a palindrome | Java program for the above approach ; Function to find the minimum cost to convert the string into a palindrome ; Length of the string ; If iointer is in the second half ; Reverse the string ; Pointing to initial position ; Find the farthest index needed to change on left side ; Find the farthest index needed to change on right side ; Changing the variable to make string palindrome ; min distance to travel to make string palindrome ; Total cost ; Return the minimum cost ; Driver Code ; Given string S ; Given pointer P ; Function Call
import java . util . * ; class GFG { static int findMinCost ( String str , int pos ) { int n = str . length ( ) ; StringBuilder input1 = new StringBuilder ( ) ; input1 . append ( str ) ; if ( pos >= n / 2 ) { input1 = input1 . reverse ( ) ; pos = n - pos - 1 ; } int left , right ; left = right = pos ; for ( int i = pos ; i >= 0 ; -- i ) { if ( input1 . charAt ( i ) != input1 . charAt ( n - i - 1 ) ) { left = i ; } } for ( int i = pos ; i < n / 2 ; ++ i ) { if ( input1 . charAt ( i ) != input1 . charAt ( n - i - 1 ) ) { right = i ; } } int ans = 0 ; for ( int i = left ; i <= right ; ++ i ) { if ( input1 . charAt ( i ) != input1 . charAt ( n - i - 1 ) ) ans += 1 ; } int dis = Math . min ( ( 2 * ( pos - left ) + ( right - pos ) ) , ( 2 * ( right - pos ) + ( pos - left ) ) ) ; ans = ans + dis ; return ans ; } public static void main ( String args [ ] ) { String S = " bass " ; int P = 3 ; System . out . println ( findMinCost ( S , P ) ) ; } }
Minimize replacements by previous or next alphabet required to make all characters of a string the same | Java program for the above approach ; Function to find the minimum count of operations to make all characters of the String same ; Set min to some large value ; Find minimum operations for each character ; Initialize cnt ; Add the value to cnt ; Update minValue ; Return minValue ; Driver Code ; Given String str ; Function call
import java . io . * ; class GFG { static int minCost ( String s , int n ) { int minValue = 100000000 ; for ( int i = 0 ; i <= 25 ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < n ; j ++ ) { cnt += Math . min ( Math . abs ( i - ( s . charAt ( j ) - ' a ' ) ) , 26 - Math . abs ( i - ( s . charAt ( j ) - ' a ' ) ) ) ; } minValue = Math . min ( minValue , cnt ) ; } return minValue ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int N = str . length ( ) ; System . out . println ( minCost ( str , N ) ) ; } }
Longest palindromic string possible by concatenating strings from a given array | Java program for the above approach ; Stores the distinct Strings from the given array ; Insert the Strings into set ; Stores the left and right subStrings of the given String ; Stores the middle subString ; Traverse the array of Strings ; Reverse the current String ; Checking if the String is itself a palindrome or not ; Check if the reverse of the String is present or not ; Append to the left subString ; Append to the right subString ; Erase both the Strings from the set ; Print the left subString ; Print the middle subString ; Print the right subString ; Driver Code ; Function Call
import java . util . * ; class GFG { static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } static void max_len ( String s [ ] , int N , int M ) { HashSet < String > set_str = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { set_str . add ( s [ i ] ) ; } Vector < String > left_ans = new Vector < > ( ) ; Vector < String > right_ans = new Vector < > ( ) ; String mid = " " ; for ( int i = 0 ; i < N ; i ++ ) { String t = s [ i ] ; t = reverse ( t ) ; if ( t == s [ i ] ) { mid = t ; } else if ( set_str . contains ( t ) ) { left_ans . add ( s [ i ] ) ; right_ans . add ( t ) ; set_str . remove ( s [ i ] ) ; set_str . remove ( t ) ; } } for ( String x : left_ans ) { System . out . print ( x ) ; } System . out . print ( mid ) ; Collections . reverse ( right_ans ) ; for ( String x : right_ans ) { System . out . print ( x ) ; } } public static void main ( String [ ] args ) { int N = 4 , M = 3 ; String s [ ] = { " omg " , " bbb " , " ffd " , " gmo " } ; max_len ( s , N , M ) ; } }
Minimize steps defined by a string required to reach the destination from a given source | Java program for the above approach ; Function to find the minimum length of string required to reach from source to destination ; Size of the string ; Stores the index of the four directions E , W , N , S ; If destination reached ; Iterate over the string ; Move east ; Change x1 according to direction E ; Move west ; Change x1 according to direction W ; Move north ; Change y1 according to direction N ; Move south ; Change y1 according to direction S ; Store the max of all positions ; Print the minimum length of string required ; Otherwise , it is impossible ; Driver Code ; Given string ; Given source and destination ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void minimum_length ( int x1 , int y1 , int x2 , int y2 , String str ) { int n = str . length ( ) ; int pos1 , pos2 , pos3 , pos4 ; pos1 = - 1 ; pos2 = - 1 ; pos3 = - 1 ; pos4 = - 1 ; if ( x1 == x2 && y1 == y2 ) { System . out . println ( "0" ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( x2 > x1 ) { if ( str . charAt ( i ) == ' E ' ) { x1 = x1 + 1 ; if ( x1 == x2 ) { pos1 = i ; } } } if ( x2 < x1 ) { if ( str . charAt ( i ) == ' W ' ) { x1 = x1 - 1 ; if ( x1 == x2 ) { pos2 = i ; } } } if ( y2 > y1 ) { if ( str . charAt ( i ) == ' N ' ) { y1 = y1 + 1 ; if ( y1 == y2 ) { pos3 = i ; } } } if ( y2 < y1 ) { if ( str . charAt ( i ) == ' S ' ) { y1 = y1 - 1 ; if ( y1 == y2 ) { pos4 = i ; } } } } int z ; z = Math . max ( pos1 , Math . max ( Math . max ( pos2 , pos3 ) , pos4 ) ) ; if ( x1 == x2 && y1 == y2 ) { System . out . println ( z + 1 ) ; } else { System . out . println ( " - 1" ) ; } } } public static void main ( String [ ] args ) throws java . lang . Exception { String str = " SESNW " ; int x1 = 0 , x2 = 1 , y1 = 0 , y2 = 1 ; minimum_length ( x1 , y1 , x2 , y2 , str ) ; } }
Remove all duplicate adjacent characters from a string using Stack | Java program to implement the above approach ; Function to remove adjacent duplicate elements ; Store the String without duplicate elements ; Store the index of str ; Traverse the String str ; Checks if stack is empty or top of the stack is not equal to current character ; If top element of the stack is equal to the current character ; If stack is empty ; If stack is not Empty ; Driver Code
import java . util . * ; class GFG { static String ShortenString ( String str1 ) { Stack < Character > st = new Stack < Character > ( ) ; int i = 0 ; while ( i < str1 . length ( ) ) { if ( st . isEmpty ( ) || str1 . charAt ( i ) != st . peek ( ) ) { st . add ( str1 . charAt ( i ) ) ; i ++ ; } else { st . pop ( ) ; i ++ ; } } if ( st . isEmpty ( ) ) { return ( " Empty ▁ String " ) ; } else { String short_String = " " ; while ( ! st . isEmpty ( ) ) { short_String = st . peek ( ) + short_String ; st . pop ( ) ; } return ( short_String ) ; } } public static void main ( String [ ] args ) { String str1 = " azzxzy " ; System . out . print ( ShortenString ( str1 ) ) ; } }
Count ways to place all the characters of two given strings alternately | Java program to implement the above approach ; Function to get the factorial of N ; Function to get the total number of distinct ways ; Length of str1 ; Length of str2 ; If both strings have equal length ; If both strings do not have equal length ; Driver code
import java . io . * ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { res = res * i ; } return res ; } static int distinctWays ( String str1 , String str2 ) { int n = str1 . length ( ) ; int m = str2 . length ( ) ; if ( n == m ) { return 2 * fact ( n ) * fact ( m ) ; } return fact ( n ) * fact ( m ) ; } public static void main ( String [ ] args ) { String str1 = " aegh " ; String str2 = " rsw " ; System . out . print ( distinctWays ( str1 , str2 ) ) ; } }
Smallest string without any multiplication sign that represents the product of two given numbers | Java program for the above approach ; Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ log <= B && 2 ^ ( log + 1 ) > B ; Update res to res += A X 2 ^ log ; Update res to res += A X 2 ^ 0 ; Find the remainder ; If remainder is not equal to 0 ; Return the resultant string ; Function to find the minimum length representation of A * B ; Find representation of form A << k1 + A << k2 + ... + A << kn ; Find representation of form B << k1 + B << k2 + ... + B << kn ; Compare the length of the representations ; Driver Code ; Product A X B ; Function Call
class GFG { public static String len ( long A , long B ) { String res = " " ; long log = 0 ; do { log = ( long ) ( Math . log ( B ) / Math . log ( 2 ) ) ; if ( log != 0 ) { res += A + " < < " + log ; } else { res += A ; break ; } B = B - ( long ) Math . pow ( 2 , log ) ; if ( B != 0 ) { res += " + " ; } else break ; } while ( log != 0 ) ; return res ; } public static void minimumString ( long A , long B ) { String res1 = len ( A , B ) ; String res2 = len ( B , A ) ; if ( res1 . length ( ) > res2 . length ( ) ) { System . out . println ( res2 ) ; } else { System . out . println ( res1 ) ; } } public static void main ( String args [ ] ) { long A = 6 ; long B = 10 ; minimumString ( A , B ) ; } }
Count substrings of a given string whose anagram is a palindrome | Java program for the above approach ; Function to print count of subStrings whose anagrams are palindromic ; Stores the answer ; Iterate over the String ; Set the current character ; Parity update ; Print the final count ; Driver Code ; Function Call
class GFG { static void countSubString ( String s ) { int res = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int x = 0 ; for ( int j = i ; j < s . length ( ) ; j ++ ) { int temp = 1 << s . charAt ( j ) - ' a ' ; x ^= temp ; if ( ( x & ( x - 1 ) ) == 0 ) res ++ ; } } System . out . print ( res ) ; } public static void main ( String [ ] args ) { String str = " aaa " ; countSubString ( str ) ; } }
Count substrings of a given string whose anagram is a palindrome | Java program for the above approach ; Function to get the count of subStrings whose anagrams are palindromic ; Store the answer ; Map to store the freq of masks ; Set frequency for mask 00. . .00 to 1 ; Store mask in x from 0 to i ; Update answer ; Update frequency ; Print the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static void countSubString ( String s ) { int answer = 0 ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; m . put ( 0 , 1 ) ; int x = 0 ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { x ^= 1 << ( s . charAt ( j ) - ' a ' ) ; answer += m . containsKey ( x ) ? m . get ( x ) : 0 ; for ( int i = 0 ; i < 26 ; ++ i ) { answer += m . containsKey ( x ^ ( 1 << i ) ) ? m . get ( x ^ ( 1 << i ) ) : 0 ; } if ( m . containsKey ( x ) ) m . put ( x , m . get ( x ) + 1 ) ; else m . put ( x , 1 ) ; } System . out . print ( answer ) ; } public static void main ( String [ ] args ) { String str = " abab " ; countSubString ( str ) ; } }
Sum of an array of large numbers | Java program for the above approach ; Function to print the result of the summation of numbers having K - digit ; Reverse the array to obtain the result ; Print every digit of the answer ; Function to calculate the total sum ; Stores the array of large numbers in integer format ; Convert each element from character to integer ; Stores the carry ; Stores the result of summation ; Initialize the sum ; Calculate sum ; Update the sum by adding existing carry ; Store the number of digits ; Increase count of digits ; If the number exceeds 9 , Store the unit digit in carry ; Store the rest of the sum ; Append digit by digit into result array ; Append result until carry is 0 ; Print the result ; Driver Code ; Given N array of large numbers
import java . util . * ; import java . lang . * ; class GFG { static void printResult ( ArrayList < Integer > result ) { Collections . reverse ( result ) ; int i = 0 ; while ( i < result . size ( ) ) { System . out . print ( result . get ( i ) ) ; i ++ ; } } static void sumOfLargeNumbers ( String v [ ] , int k , int N ) { ArrayList < ArrayList < Integer > > x = new ArrayList < > ( 1000 ) ; for ( int i = 0 ; i < k ; i ++ ) x . add ( new ArrayList < Integer > ( ) ) ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { x . get ( i ) . add ( v [ i ] . charAt ( j ) - '0' ) ; } } int carry = 0 ; ArrayList < Integer > result = new ArrayList < > ( ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) { int sum = 0 ; for ( int j = 0 ; j < k ; j ++ ) sum += x . get ( j ) . get ( i ) ; sum += carry ; int temp = sum ; int count = 0 ; while ( temp > 9 ) { temp = temp % 10 ; count ++ ; } long l = ( long ) Math . pow ( 10 , count ) ; if ( l != 1 ) carry = ( int ) ( sum / l ) ; sum = sum % 10 ; result . add ( sum ) ; } while ( carry != 0 ) { int a = carry % 10 ; result . add ( a ) ; carry = carry / 10 ; } printResult ( result ) ; } public static void main ( String [ ] args ) { int K = 10 ; int N = 5 ; String arr [ ] = { "1111111111" , "1111111111" , "1111111111" , "1111111111" , "1111111111" } ; sumOfLargeNumbers ( arr , N , K ) ; } }
Reverse words in a given string | Set 2 | Java Program to implement the above approach ; Function to reverse the words of a given String ; Stack to store each word of the String ; Store the whole String in String stream ; Push each word of the String into the stack ; Print the String in reverse order of the words ; Driver Code
import java . util . * ; class GFG { static void printRev ( String str ) { Stack < String > st = new Stack < String > ( ) ; String [ ] ss = str . split ( " ▁ " ) ; for ( String temp : ss ) { st . add ( temp ) ; } while ( ! st . isEmpty ( ) ) { System . out . print ( st . peek ( ) + " ▁ " ) ; st . pop ( ) ; } } public static void main ( String [ ] args ) { String str ; str = " geeks ▁ quiz ▁ practice ▁ code " ; printRev ( str ) ; } }
Count of substrings of a Binary string containing only 1 s | Java implementation to find count of substring containing only ones ; Function to find the total number of substring having only ones ; Driver code
class GFG { static int countOfSubstringWithOnlyOnes ( String s ) { int res = 0 , count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { count = s . charAt ( i ) == '1' ? count + 1 : 0 ; res = ( res + count ) ; } return res ; } public static void main ( String [ ] args ) { String s = "0110111" ; System . out . println ( countOfSubstringWithOnlyOnes ( s ) ) ; } }
Count of Distinct strings possible by inserting K characters in the original string | Java program for the above approach ; Function to calculate and return x ^ n in log ( n ) time using Binary Exponentiation ; Function to calculate the factorial of a number ; Function to calculate combination ; nCi = ( n ! ) / ( ( n - i ) ! * i ! ) ; Using Euler 's theorem of Modular  multiplicative inverse to find  the inverse of a number.  (1/a)%mod=a^(m?2)%mod ; Function to find the count of possible strings ; Number of ways to form all possible strings ; Number of ways to form strings that don 't contain the input  string as a subsequence ; Checking for all prefix length from 0 to | S | - 1. ; To calculate nCi ; Select the remaining characters 25 ^ ( N - i ) ; Add the answer for this prefix length to the final answer ; Answer is the difference of allWays and noWays ; Print the answer ; Driver code
class GFG { static final long mod = 1000000007 ; static long binExp ( long base , long power ) { long x = 1 ; while ( power != 0 ) { if ( power % 2 == 1 ) x = ( ( x % mod ) * ( base % mod ) ) % mod ; base = ( ( base % mod ) * ( base % mod ) ) % mod ; power = power / 2 ; } return x ; } static long fact ( long num ) { long result = 1 ; for ( long i = 1 ; i <= num ; ++ i ) { result = ( ( result % mod ) * ( i % mod ) ) % mod ; } return result ; } static long calculate_nCi ( long N , long i ) { long nfact = fact ( N ) ; long ifact = fact ( i ) ; long dfact = fact ( N - i ) ; long inv_ifact = binExp ( ifact , mod - 2 ) ; long inv_dfact = binExp ( dfact , mod - 2 ) ; long denm = ( ( inv_ifact % mod ) * ( inv_dfact % mod ) ) % mod ; long answer = ( ( nfact % mod ) * ( denm % mod ) ) % mod ; return answer ; } static void countSubstring ( long N , long s , long k ) { long allWays = binExp ( 26 , N ) ; long noWays = 0 ; for ( long i = 0 ; i < s ; ++ i ) { long nCi = calculate_nCi ( N , i ) ; long remaining = binExp ( 25 , N - i ) ; long multiply = ( ( nCi % mod ) * ( remaining % mod ) ) % mod ; noWays = ( ( noWays % mod ) + ( multiply % mod ) ) % mod ; } long answer = ( ( allWays % mod ) - ( noWays % mod ) ) % mod ; if ( answer < 0 ) answer += mod ; System . out . println ( answer ) ; } public static void main ( String [ ] args ) { String str = " abc " ; long k = 2 ; long s = str . length ( ) ; long N = s + k ; countSubstring ( N , s , k ) ; } }
Calculate Sum of ratio of special characters to length of substrings of the given string | Java Program to implement the above approach ; Stores frequency of special characters in the array ; Stores prefix sum ; Function to check whether a character is special or not ; If current character is special ; Otherwise ; Function to find sum of ratio of count of special characters and length of subStrings ; Calculate the prefix sum of special nodes ; Generate prefix sum array ; Calculate ratio for subString ; Driver Code ;
import java . util . * ; class GFG { static int N = 1000000 + 5 ; static int [ ] prefix = new int [ N ] ; static int [ ] sum = new int [ N ] ; static int isSpecial ( char c , char [ ] special ) { for ( char i : special ) if ( i == c ) return 1 ; return 0 ; } static double countRatio ( char [ ] s , char [ ] special ) { int n = s . length ; for ( int i = 0 ; i < n ; i ++ ) { prefix [ i ] = ( isSpecial ( s [ i ] , special ) ) ; if ( i > 0 ) prefix [ i ] += prefix [ i - 1 ] ; } for ( int i = 0 ; i < n ; i ++ ) { sum [ i ] = prefix [ i ] ; if ( i > 0 ) sum [ i ] += sum [ i - 1 ] ; } double ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int count = sum [ n - 1 ] - ( i > 1 ? sum [ i - 2 ] : 0 ) ; count -= ( i < n ? sum [ n - i - 1 ] : 0 ) ; ans += ( double ) count / ( double ) i ; } return ans ; } public static void main ( String [ ] args ) { String s = " abcd " ; char [ ] special = { ' b ' , ' c ' } ; double ans = countRatio ( s . toCharArray ( ) , special ) ; System . out . format ( " % .6f " , ans ) ; } }
Minimum replacements in a string to make adjacent characters unequal | Java program to find minimum replacements in a string to make adjacent characters unequal ; Function which counts the minimum number of required operations ; n stores the length of the string s ; ans will store the required ans ; i is the current index in the string ; Move j until characters s [ i ] & s [ j ] are equal or the end of the string is reached ; diff stores the length of the substring such that all the characters are equal in it ; We need atleast diff / 2 operations for this substring ; Driver code
import java . util . * ; class GFG { static void count_minimum ( String s ) { int n = s . length ( ) ; int ans = 0 ; int i = 0 ; while ( i < n ) { int j = i ; while ( j < n && s . charAt ( j ) == s . charAt ( i ) ) { j ++ ; } int diff = j - i ; ans += diff / 2 ; i = j ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String str = " caaab " ; count_minimum ( str ) ; } }
Minimum cost to convert given string to consist of only vowels | Java program for the above approach ; Function to find the minimum cost ; Store vowels ; Loop for iteration of string ; Loop to calculate the cost ; Add minimum cost ; Driver Code ; Given string ; Function call
import java . io . * ; import java . util . * ; class GFG { static int min_cost ( String st ) { String vow = " aeiou " ; int cost = 0 ; for ( int i = 0 ; i < st . length ( ) ; i ++ ) { ArrayList < Integer > costs = new ArrayList < > ( ) ; for ( int j = 0 ; j < 5 ; j ++ ) costs . add ( Math . abs ( st . charAt ( i ) - vow . charAt ( j ) ) ) ; int minx = Integer . MAX_VALUE ; for ( int x : costs ) { if ( x < minx ) { minx = x ; } } cost += minx ; } return cost ; } public static void main ( String [ ] args ) { String str = " abcde " ; System . out . println ( min_cost ( str ) ) ; } }