Minimum cost to convert given string to consist of only vowels | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_cost ( string st ) { string vow = " aeiou " ; int cost = 0 ; for ( int i = 0 ; i < st . size ( ) ; i ++ ) { vector < int > costs ; for ( int j = 0 ; j < 5 ; j ++ ) costs . push_back ( abs ( st [ i ] - vow [ j ] ) ) ; cost += * min_element ( costs . begin ( ) , costs . end ( ) ) ; } return cost ; } int main ( ) { string str = " abcde " ; cout << ( min_cost ( str ) ) ; }