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 ) ) ; } }